-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend.py
69 lines (60 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
67
68
69
import sqlite3
def connect():
"""
It creates a database called books.db and creates a table called books.
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, title text,author text,year integer,price integer)")
conn.commit()
conn.close()
def insert(title, author, year,price):
"""
It takes in 4 parameters (title, author, year, price) and inserts them into the database
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("INSERT INTO books VALUES(NULL,?,?,?,?)",(title,author,year,price))
conn.commit()
conn.close()
def view():
"""
It connects to the database, fetches all the items in db, closes the connection, and
returns the results.
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("SELECT * FROM books")
rows=cur.fetchall()
conn.close()
return rows
def search(title="",author="",year="",price=""):
"""
It requires atleast one parameter to be passed in. It connects to the database, fetches all the items in db with particular search criteria, closes the connection, and returns the results.
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR price=?",(title,author,year,price))
rows=cur.fetchall()
conn.close()
return rows
def delete(id):
"""
It takes the id of the book as an argument and deletes the book from the database
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("DELETE FROM books WHERE id=?",(id,))
conn.commit()
conn.close()
def update(id,title,author,year,price):
"""
It takes the id of the book, and the new values for the title, author, year, and price, and updates
the database with the new values
"""
conn=sqlite3.connect("books.db")
cur=conn.cursor()
cur.execute("UPDATE books SET title=?, author=?, year=?, price=? WHERE id=?",(title,author,year,price,id))
conn.commit()
conn.close()
connect()