sqlite3 interface for addition of books
This commit is contained in:
parent
d8848cdb0b
commit
c390335a87
|
@ -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)
|
Loading…
Reference in New Issue