-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
82 lines (58 loc) · 1.8 KB
/
db.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
70
71
72
73
74
75
76
77
78
79
80
81
82
import sqlite3
def getAll():
conn = sqlite3.connect('db.sqlite3')
print "Opened database successfully";
cursor = conn.execute("SELECT ID,NAME,PRICE from PRODUCT")
rows = [x for x in cursor]
cols = [x[0] for x in cursor.description]
data = []
for row in rows:
entry = {}
for prop, val in zip(cols, row):
entry[prop] = val
data.append(entry)
print "Operation done successfully";
conn.close()
return data
def getOne(id):
conn = sqlite3.connect('db.sqlite3')
print "Opened database successfully";
cursor = conn.execute("SELECT ID,NAME,DESCRIPTION,PRICE from PRODUCT where ID=%s" % id)
rows = [x for x in cursor]
cols = [x[0] for x in cursor.description]
data = []
for row in rows:
entry = {}
for prop, val in zip(cols, row):
entry[prop] = val
data.append(entry)
print "Operation done successfully";
conn.close()
return data
def putOne(name, description, price):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("INSERT INTO PRODUCT ('NAME', 'DESCRIPTION', 'PRICE') VALUES(?,?,?)", (name, description, price))
conn.commit()
print "Operation done successfully";
c.close()
conn.close()
return
def updateOne(id, name, description, price):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("UPDATE PRODUCT SET NAME=?, DESCRIPTION=?, PRICE=? WHERE ID=?", (name, description, price, id))
conn.commit()
print "Operation done successfully";
c.close()
conn.close()
return
def delete(id):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
c.execute("DELETE FROM PRODUCT WHERE ID=%s" % id)
conn.commit()
print "Operation done successfully";
c.close()
conn.close()
return