-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
68 lines (61 loc) · 2.08 KB
/
backend.py
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
import sqlite3
def connect():
conn = sqlite3.connect("books.db")
cur = conn.cursor()
cur.execute("create table if not exists book(id integer primary key, title text, author text, year integer, isbn varchar(20))")
conn.commit()
conn.close()
def insert(title, author, year, isbn):
conn = sqlite3.connect("books.db")
cur = conn.cursor()
cur.execute("insert into book values(NULL,?,?,?,?)", (title, author, year, isbn))
conn.commit()
conn.close()
def view():
conn = sqlite3.connect("books.db")
cur = conn.cursor()
cur.execute("select * from book")
rows = cur.fetchall()
conn.close()
return rows
def search(title="", author="", year="", isbn=""):
conn = sqlite3.connect("books.db")
cur = conn.cursor()
query = "select * from book where"
if title:
if len(query) > 24:
query += ' and lower(title) like lower("%{}%")'.format(title)
else:
query += ' lower(title) like("%{}%")'.format(title)
if author:
if len(query) > 24:
query += ' and lower(author) like lower("%{}%")'.format(author)
else:
query += ' lower(author) like("%{}%")'.format(author)
if year:
if len(query) > 24:
query += ' and year={}'.format(year)
else:
query += ' year={}'.format(year)
if isbn:
if len(query) > 24:
query += ' and lower(isbn) like lower("%{}%")'.format(isbn)
else:
query += ' lower(isbn) like("%{}%")'.format(isbn)
cur.execute(query)
rows = cur.fetchall()
conn.close()
return rows
def delete(id):
conn = sqlite3.connect("books.db")
cur = conn.cursor()
cur.execute("delete from book where id=?", (id,))
conn.commit()
conn.close()
def update(id, title, author, year, isbn):
conn = sqlite3.connect("books.db")
cur = conn.cursor()
cur.execute("update book set title=?, author=?, year=?, isbn=? where id=?",(title, author, year, isbn, id))
conn.commit()
conn.close()
connect()