1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
#   File: LibrarySql.py
# Author: shadytyrant@gmail.com
#   Date: 2009-11-25
#  Notes: a module that handles SQL querys for the library database
#         supports adding, removing, editing, and searching information.
#--------------------------------------------
 
import sqlite3
 
class Library:
    def __init__(self, database):
        # Class variables
        self.GENRA = ('fiction''non-fiction''science fiction''technical''reference')
 
        # Open database
        self.connect = sqlite3.connect(database)
        self.cursor = self.connect.cursor()
 
 
    def Perform(self, sqlStatment):
        self.cursor.execute(sqlStatment)
        self.connect.commit()
 
 
    def CheckId(self, ID):
        sqlStatment = 'SELECT * FROM library WHERE id = (%d)' % int(ID)
        self.Perform(sqlStatment)
 
        info = ''
        for row in self.cursor:
            info += str(row)
 
        if not info:
            errorString = 'No book with id of %d' % int(ID)
            raise LibraryError(errorString)
 
 
    def CloseConnection(self):
        self.cursor.close()
        self.connect.commit()
 
    #--------------------------------------------
 
    def SearchAll(self):
        sqlStatment = 'SELECT * FROM library ORDER BY id'
        self.Perform(sqlStatment)
 
        allData = []
        for row in self.cursor:
            allData.append(row)
 
        if not allData:
            raise LibraryError("No books found in library")
        else:
            return allData
 
    def SearchByTitle(self, title):
        try:
            sqlStatment = 'SELECT * FROM library WHERE title = ("%s")' % title
            self.Perform(sqlStatment)
        except(ValueError):
            raise LibraryError("Invalid Search")
 
        allData = []
        for row in self.cursor:
            allData.append(row)
 
        if not allData:
            errorString = 'No books with title of %s' % title
            raise LibraryError(errorString)
        else:
            return allData
 
    def AddBook(self, title, author, genra, publisher, seriesIndex=1):
        try:
            sqlStatment = 'INSERT INTO library VALUES("%s", "%d", "%s", NULL, "%s", "%s")' % (genra, seriesIndex, author, publisher, title)
            self.Perform(sqlStatment)
        except:
            raise LibraryError('Could not add book to library')
 
 
    def RemoveBook(self, ID):
        self.CheckId(int(ID))
        try:
            sqlStatment = 'DELETE FROM library WHERE id = (%d)' % int(ID)
            self.Perform(sqlStatment)
        except:
            raise LibraryError('Could not remove book from library')
 
#--------------------------------------------
 
class LibraryError(Exception):
   def __init__(self, value):
       self.parameter = value
 
   def __str__(self):
       return repr(self.parameter)