-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
45 lines (36 loc) · 1.45 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
import sqlite3
class Database:
# Connecting to the database
def __init__(self, db):
self.conn=sqlite3.connect(db)
self.cur=self.conn.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)")
self.conn.commit()
# Insert function
def insert(self,title,author,year,isbn):
self.cur.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title,author,year,isbn))
self.conn.commit()
# View function
def view(self):
self.cur.execute("SELECT * FROM book")
return self.cur.fetchall()
# Search function
def search(self,title="",author="",year="",isbn=""):
self.cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?", (title,author,year,isbn))
return self.cur.fetchall()
# Delete function
def delete(self,id):
self.cur.execute("DELETE FROM book WHERE id=?",(id,))
self.conn.commit()
# Update function
def update(self,id,title,author,year,isbn):
self.cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?",(title,author,year,isbn,id))
self.conn.commit()
# Delete all function
def __del__(self):
self.conn.close()
#insert("The Sun","John Smith",1918,913123132)
#delete(3)
#update(4,"The moon","John Smooth",1917,99999)
#print(view())
#print(search(author="John Smooth"))