One function to convert query result to a book.
This commit is contained in:
parent
45dafba38a
commit
e05c6c157b
44
db_layer.py
44
db_layer.py
|
@ -92,15 +92,7 @@ def getBooks():
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
query = "SELECT * FROM "+bookTable+" WHERE deleted=0;"
|
query = "SELECT * FROM "+bookTable+" WHERE deleted=0;"
|
||||||
c.execute(query)
|
c.execute(query)
|
||||||
books = []
|
books = [_query_to_book(b) for b in c]
|
||||||
for b in c:
|
|
||||||
book = {}
|
|
||||||
i = 0
|
|
||||||
for k in columns:
|
|
||||||
if b[i]!=None:
|
|
||||||
book[k]=b[i]
|
|
||||||
i+=1
|
|
||||||
books.append(book)
|
|
||||||
c.close()
|
c.close()
|
||||||
return books
|
return books
|
||||||
|
|
||||||
|
@ -109,15 +101,7 @@ def getBooksByCategory(cat):
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
query = "SELECT "+",".join(map(colify,columns))+" FROM "+bookTable+" JOIN "+bookCategoryTable+" USING (id) WHERE cat_id = :id AND deleted=0;"
|
query = "SELECT "+",".join(map(colify,columns))+" FROM "+bookTable+" JOIN "+bookCategoryTable+" USING (id) WHERE cat_id = :id AND deleted=0;"
|
||||||
c.execute(query,cat)
|
c.execute(query,cat)
|
||||||
books = []
|
books = [_query_to_book(b) for b in c]
|
||||||
for b in c:
|
|
||||||
book = {}
|
|
||||||
i = 0
|
|
||||||
for k in columns:
|
|
||||||
if b[i]!=None:
|
|
||||||
book[k]=b[i]
|
|
||||||
i+=1
|
|
||||||
books.append(book)
|
|
||||||
c.close()
|
c.close()
|
||||||
return books
|
return books
|
||||||
|
|
||||||
|
@ -126,15 +110,7 @@ def getRemovedBooks():
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
query = "SELECT * FROM "+bookTable+" WHERE DELETED=1;"
|
query = "SELECT * FROM "+bookTable+" WHERE DELETED=1;"
|
||||||
c.execute(query)
|
c.execute(query)
|
||||||
books = []
|
books = [_query_to_book(b) for b in c]
|
||||||
for b in c:
|
|
||||||
book = {}
|
|
||||||
i = 0
|
|
||||||
for k in columns:
|
|
||||||
if b[i]!=None:
|
|
||||||
book[k]=b[i]
|
|
||||||
i+=1
|
|
||||||
books.append(book)
|
|
||||||
c.close()
|
c.close()
|
||||||
return books
|
return books
|
||||||
|
|
||||||
|
@ -143,17 +119,10 @@ def getBookByID(bookid):
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
query = "SELECT * FROM "+bookTable+" WHERE id = "+str(bookid)+";"
|
query = "SELECT * FROM "+bookTable+" WHERE id = "+str(bookid)+";"
|
||||||
c.execute(query)
|
c.execute(query)
|
||||||
b = c.fetchone()
|
book = _query_to_book(c.fetchone())
|
||||||
book = {}
|
|
||||||
i=0
|
|
||||||
for k in columns:
|
|
||||||
if b[i]!=None:
|
|
||||||
book[k]=b[i]
|
|
||||||
i+=1
|
|
||||||
c.close()
|
c.close()
|
||||||
return book
|
return book
|
||||||
|
|
||||||
|
|
||||||
# removes book from catalogue
|
# removes book from catalogue
|
||||||
def removeBook(bookid):
|
def removeBook(bookid):
|
||||||
conn = sqlite3.connect(dbFile)
|
conn = sqlite3.connect(dbFile)
|
||||||
|
@ -200,6 +169,11 @@ def deleteBooks(books):
|
||||||
conn.commit()
|
conn.commit()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
|
def _query_to_book(book_query):
|
||||||
|
# Make a dict out of column name and query results.
|
||||||
|
# Empty entries return None, which are removed from the dict.
|
||||||
|
return dict(filter(lambda t:t[1], zip(columns,book_query)))
|
||||||
|
|
||||||
#########################################
|
#########################################
|
||||||
# Category related functions
|
# Category related functions
|
||||||
########################################
|
########################################
|
||||||
|
|
|
@ -85,8 +85,8 @@ def addForm():
|
||||||
bf = form.bookForm(w,hb)
|
bf = form.bookForm(w,hb)
|
||||||
(r,c)=w.getmaxyx()
|
(r,c)=w.getmaxyx()
|
||||||
w.mvwin((my-r)//2,(mx-c)//2)
|
w.mvwin((my-r)//2,(mx-c)//2)
|
||||||
bf.lookup_isbn=bookData.openLibrary_isbn
|
bf.lookup_isbn=book_data.openLibrary_isbn
|
||||||
bf.lookup_lccn=bookData.openLibrary_lccn
|
bf.lookup_lccn=book_data.openLibrary_lccn
|
||||||
bf.caption='Add a Book'
|
bf.caption='Add a Book'
|
||||||
bf.blabel = 'Add'
|
bf.blabel = 'Add'
|
||||||
book = bf.eventLoop()
|
book = bf.eventLoop()
|
||||||
|
|
Loading…
Reference in New Issue