now adds books to database
[public/library.git] / sqCatalogue.py
1 import sqlite3
2
3 dbFile = 'sqLibrary.db'
4 bookTable = 'books'
5
6 bookTableCreation = '''
7 CREATE TABLE IF NOT EXISTS books
8     (id INTEGER PRIMARY KEY, 
9      isbn, lccn, title, subtitle, authors, edition, 
10      publisher, publish_year, publish_month, publish_location, 
11      pages, pagination, weight)
12 '''
13
14 def colify(s):
15     return s.replace(" ","_").lower()
16
17 # escapes strings and such
18 def stringify(v):
19     return '"' + str(v).strip().replace('"','""') + '"'
20
21 def addBook(book):
22     conn = sqlite3.connect(dbFile)
23     c = conn.cursor()
24     cols = []
25     vals = []
26     for k,v in book.items():
27         if v!="":
28             cols.append(colify(k))
29             vals.append(stringify(v))
30     
31     query = "INSERT INTO "+bookTable+" ("+", ".join(cols)+") VALUES ("+", ".join(vals)+");"
32     c.execute(query)
33     conn.commit()
34     c.close()
35
36 def createBooksTable():
37     conn = sqlite3.connect(dbFile)
38     c = conn.cursor()
39     c.execute(bookTableCreation)
40     conn.commit()
41     c.close()
42
43 createBooksTable()