-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLite_test.py
231 lines (198 loc) · 6.43 KB
/
SQLite_test.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
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
225
226
227
228
229
230
231
#!/usr/bin/python
import sqlite3
import sys
from sqlite3 import Error
def create_connection():
""" create a connection to the SQLite
specified by the db_file
:param db_file: file
:return: Connection object or None
"""
db_file= "gro_log.db"
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
def profile_table_setup():
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profiles (name STRING NOT NULL,user_db STRING NOT NULL)")
def create_profile(name):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("INSERT or IGNORE INTO profiles (name,user_db) values (\""+str(name)+"\",\""+str(name.lower())+"_db\")")
cur=conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS "+str(name.lower())+"_db(barcode INTEGER,item_name STRING NOT NULL, exp DATE)")
def delete_profile(name):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("DELETE FROM profiles WHERE name =\""+name+"\"")
cur=conn.cursor()
cur.execute("DROP TABLE "+ str(name.lower())+"_db")
def select_all_profiles():
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("SELECT * FROM profiles")
rows = cur.fetchall()
array=[]
for row in rows:
array.append(row[0])
return array
def count_exp(name):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM `"+str(name.lower())+"_db` WHERE exp < date('now', '+7 days');")
rows = cur.fetchall()
return rows[0][0]
def select_inventory(name):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("SELECT * FROM "+str(name.lower())+"_db")
rows = cur.fetchall()
array=[]
max_len=0
for row in rows:
if len(row[1]) > max_len:
max_len=len(row[1])
for row in rows:
temp=str(row[1])
x=0
pad=" "
length= len(temp)
if length < max_len:
temp=(pad*(max_len-length)*2)+temp
if length== max_len:
temp=(pad*(40-length))+temp
while x <= (40-length+max_len):
temp= temp+pad
x+=1
temp=temp+str(row[2])
if len(str(row[2]))<5:
temp=temp+pad*(12-len(str(row[2])))
temp=pad*(int(length/2)*int(max_len/length))+temp
temp=temp+pad*48+str(row[0])
if len(str(row[2]))<5:
temp=temp+pad*(len(temp)-230)
if len(str(row[0]))<9:
temp=temp+pad*(20-len(str(row[0])))
if length < max_len:
temp=temp+(pad*((max_len-length)+length))
if length == max_len:
temp=temp+pad*(40-length)
temp=temp+pad*(max_len)
array.append(temp)
return array
def add_inventory(name,item_info):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("INSERT INTO "+str(name.lower())+"_db (barcode,item_name,exp) values ("+str(item_info[1])+",\""+str(item_info[0])+"\",\""+str(item_info[2])+"\")")
def add_other_inventory(name,item_info):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("INSERT INTO "+str(name.lower())+"_db (item_name,exp) values (\""+str(item_info[0])+"\",\""+str(item_info[1])+"\")")
def delete_inventory(name,item_name):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("DELETE FROM "+str(name.lower())+"_db WHERE item_name LIKE \"%"+item_name+"%\"")
def select_profile_db(name):
"""
Query tasks by priority
:param conn: the Connection object
:param name: name of profile wanted
:return:
"""
conn = create_connection()
with conn:
cur = conn.cursor()
cur.execute("SELECT name FROM profiles WHERE name=\""+name+"\"")
rows = cur.fetchall()
array=[]
for row in rows:
array.append(row[0])
print(array)
return array
#def main():
#profile_table_setup()
#--------Add Profile Test------------
#add = input("Name you would like to add:")
#create_profile(add)
#print(select_all_profiles())
#-------Delete Profile Test----------
#delete= input("Name you would like to delete:")
#delete_profile(conn,delete)
#-------Select Profile Test----------
#name= raw_input("Name you would like to select:")
#select_profile_db(conn,name)
#-------Show All Profiles Test-------
#print("\n2. Query all profiles")
#select_all_profiles(conn)
#-------Add Item Test---------
#name=input("Database name: ")
#barcode=input("Barcode: ")
#item_name=input("Item name: ")
#exp_date=input("Exp Date(YYYY-MM-DD): ")
#add_inventory(name,barcode,item_name,exp_date)
#select_inventory("Zack")
#if __name__ == '__main__':
# main()