-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
66 lines (61 loc) · 2.11 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
import sqlite3
def connect():
"""Set up a connection with the database."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("CREATE TABLE IF NOT EXISTS "
"book (id integer PRIMARY KEY, "
"title text, "
"author text, "
"year integer, "
"isbn integer)")
conn_obj.commit()
conn_obj.close()
def insert(title, author, year, isbn):
"""Insert entry into database."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("INSERT INTO book "
"VALUES (NULL, ?, ?, ?, ?)", (title, author, year, isbn))
conn_obj.commit()
conn_obj.close()
def view():
"""View all database entries."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("SELECT * FROM book")
rows = cur_obj.fetchall()
conn_obj.close()
return rows
def update(id, title, author, year, isbn):
"""Update a database entry."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("UPDATE book "
"SET title = ?, "
"author = ?, "
"year = ?, "
"isbn = ? "
"WHERE id = ?",
(title, author, year, isbn, id))
conn_obj.commit()
conn_obj.close()
def delete(d):
"""Delete a database entry."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("DELETE FROM book WHERE id = ?",(d,))
conn_obj.commit()
conn_obj.close()
def search(title = "", author = "", year = "", isbn = ""):
"""Search for a database entry."""
conn_obj = sqlite3.connect("books.db")
cur_obj = conn_obj.cursor()
cur_obj.execute("SELECT * "
"FROM book "
"WHERE title = ? OR author = ? OR year = ? OR isbn = ?",
(title, author, year, isbn))
rows = cur_obj.fetchall()
conn_obj.close()
return rows
connect()