-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb.py
More file actions
224 lines (183 loc) · 6.6 KB
/
db.py
File metadata and controls
224 lines (183 loc) · 6.6 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import sqlite3
import datetime
now = datetime.datetime.utcnow()
CREATE_GROCERIES = "CREATE TABLE IF NOT EXISTS groceries (id INTEGER PRIMARY KEY,good TEXT, price INTEGER, date DATE);"
CREATE_HOUSEHOLD = "CREATE TABLE IF NOT EXISTS household (id INTEGER PRIMARY KEY,good TEXT, price INTEGER, date DATE);"
CREATE_ENTERTAINMENT = "CREATE TABLE IF NOT EXISTS entertainment (id INTEGER PRIMARY KEY,good TEXT, price INTEGER, date DATE);"
CREATE_OTHER = "CREATE TABLE IF NOT EXISTS other (id INTEGER PRIMARY KEY,good TEXT, price INTEGER, date DATE);"
INSERT_GROCERIES = "INSERT INTO groceries (good, price, date) VALUES(?,?,?);"
INSERT_HOUSEHOLD = "INSERT INTO household (good, price, date) VALUES(?,?,?);"
INSERT_ENTERTAINMENT = "INSERT INTO entertainment (good, price, date) VALUES(?,?,?);"
INSERT_OTHER = "INSERT INTO other (good, price, date) VALUES(?,?,?);"
SELECT_ALL1 = "SELECT * FROM groceries;"
SELECT_ALL2 = "SELECT * FROM household;"
SELECT_ALL3 = "SELECT * FROM entertainment;"
SELECT_ALL4 = "SELECT * FROM other;"
SELECT_GROCERIES = "SELECT * FROM groceries WHERE good = ? AND price = ?;"
SELECT_HOUSEHOLD = "SELECT * FROM household WHERE good = ? AND price = ?;"
SELECT_ENTERTAINMENT = "SELECT * FROM entertainment WHERE good = ? AND price = ?;"
SELECT_OTHER = "SELECT * FROM other WHERE good = ? AND price = ?;"
DELETE_GROCERIES = "DELETE FROM groceries WHERE good = ? AND price = ?;"
DELETE_HOUSEHOLD = "DELETE FROM household WHERE good = ? AND price = ?;"
DELETE_ENTERTAINMENT = "DELETE FROM entertainment WHERE good = ? AND price = ?;"
DELETE_OTHER = "DELETE FROM other WHERE good = ? AND price = ?;"
###create for every table###
def create_tables():
conn = sqlite3.connect('data.db')
with conn:
return conn.execute(CREATE_OTHER)
###INSERT VALUES###
def insert_groceries(good, price, date):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(INSERT_GROCERIES, (good, price, date))
conn.commit()
def insert_household(good, price, date):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(INSERT_HOUSEHOLD, (good, price, date))
conn.commit()
c.close()
def insert_entertrainment(good, price, date):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(INSERT_ENTERTAINMENT, (good, price, date))
conn.commit()
c.close()
def insert_other(good, price, date):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(INSERT_OTHER, (good, price, date))
conn.commit()
c.close()
###SELECT_ALL###
def select_all_groceries():
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_ALL1)
#have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_all_household():
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_ALL2)
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_all_entertrainment():
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_ALL3)
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_all_other():
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_ALL4)
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
###SELECT SPECIFIC###
def select_grocery(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_GROCERIES, (good, price))
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_household(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_HOUSEHOLD, (good, price))
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_entertainment(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_ENTERTAINMENT, (good, price))
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
def select_other(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(SELECT_OTHER, (good, price))
# have to store data into a list of Tuple
list = c.fetchall()
c.close()
output = ''
for x in list:
output = output + str(x[1]) + ' ' + str(x[2]) + ' ' + ' ' + str(x[3]) + '\n'
return output
###DELETE VALUE###
def delete_grocery(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(DELETE_GROCERIES, (good, price))
conn.commit()
c.close()
def delete_household(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(DELETE_HOUSEHOLD, (good, price))
conn.commit()
c.close()
def delete_entertainment(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(DELETE_ENTERTAINMENT, (good, price))
conn.commit()
c.close()
def delete_other(good, price):
conn = sqlite3.connect('data.db')
with conn:
c = conn.cursor()
c.execute(DELETE_OTHER, (good, price))
conn.commit()
c.close()