sqlite3 interface for addition of books
authorJohn Ladan <jladan@uwaterloo.ca>
Sat, 17 Mar 2012 06:01:02 +0000 (02:01 -0400)
committerJohn Ladan <jladan@uwaterloo.ca>
Sat, 17 Mar 2012 06:01:02 +0000 (02:01 -0400)
sqCatalogue.py [new file with mode: 0644]

diff --git a/sqCatalogue.py b/sqCatalogue.py
new file mode 100644 (file)
index 0000000..3cb0444
--- /dev/null
@@ -0,0 +1,47 @@
+import sqlite3
+
+dbFile = 'sqLibrary.db'
+bookTable = 'books'
+
+bookTableCreation = '''
+CREATE TABLE IF NOT EXISTS books
+    (id INTEGER PRIMARY KEY, 
+     isbn, lccn, title, subtitle, authors, edition, 
+     publisher, publish_year, publish_month, publish_location, 
+     pages, pagination, weight)
+'''
+
+def colify(s):
+    return s.replace(" ","_").lower()
+
+def stringify(v):
+    return '"' + str(v).strip() + '"'
+
+def addBook(book):
+    conn = sqlite3.connect(dbFile)
+    c = conn.cursor()
+    cols = []
+    vals = []
+    for k,v in book.items():
+        if v!="":
+            cols.append(colify(k))
+            vals.append(stringify(v))
+    
+    query = "INSERT INTO "+bookTable+" ("+", ".join(cols)+") VALUES ("+", ".join(vals)+");"
+    print query
+    c.execute(query)
+    conn.commit()
+    c.close()
+
+def createBooksTable():
+    conn = sqlite3.connect(dbFile)
+    c = conn.cursor()
+    c.execute(bookTableCreation)
+    conn.commit()
+    c.close()
+
+createBooksTable()
+book1 = {"title":"Test Book 1"}
+book2 = {"isbn":12345, "title":"Test Book 2"}
+addBook(book1)
+addBook(book2)