· 9 years ago · Jan 23, 2017, 11:32 AM
1import sqlite3
2
3DB_FILE = 'database.db'
4
5def db_init():
6 """
7 Initialize the sqlite3 database if it doesn't exist or the tables are
8 missing..
9 :return: n/a
10 """
11 conn = sqlite3.connect(DB_FILE)
12 c = conn.cursor()
13
14 c.execute(
15 'CREATE TABLE if not exists results \
16 (tag text, num integer, status text')
17
18 conn.commit()
19 conn.close()
20
21def db_write(tag, num, status):
22 global DEBUG
23 """
24 Writes results from a single boot to the database.
25 :param data: Data to write.
26 :param boot: Boot count.
27 :return: n/a
28 """
29 conn = sqlite3.connect(DB_FILE)
30 c = conn.cursor()
31
32 row = (tag, num, status)
33
34 c.execute('INSERT INTO results VALUES (?, ?, ?)', row)
35 conn.commit()
36 conn.close()
37
38def db_read(key, filter_value):
39 if not os.path.isfile(DB_FILE):
40 print('No db file detected!')
41 exit()
42
43 conn = sqlite3.connect(DB_FILE)
44 conn.row_factory = sqlite3.Row
45
46 c = conn.cursor()
47
48 cmd = 'SELECT * FROM results'
49 if key != '':
50 cmd = cmd + ' WHERE tag = \"%s\" ' % key
51
52 try:
53 c.execute(cmd)
54 except sqlite3.OperationalError:
55 print cmd
56 tbl = from_db_cursor(c)
57 print(tbl)
58
59 conn.close()
60
61 def db_delete_tag(key):
62 conn = sqlite3.connect(DB_FILE)
63 conn.row_factory = sqlite3.Row
64 c = conn.cursor()
65
66 if DEBUG: print('Deleting %s from database' % key)
67 c.execute('DELETE FROM results WHERE tag = \'%s\'' % key)
68
69 conn.commit()
70 conn.close()