-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbi.py
executable file
·77 lines (65 loc) · 1.83 KB
/
dbi.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
#!/bin/python3
#// Author: Thomas Grothe, [email protected]
#// 2024/2/22
import sys
import csv
import sqlite3 as sq
#import pygresql #possibly implement later
#Generic database interface. to store python dicts on a local sqlite db
#
class DBI:
#params:
# d = an example dict you intend to store
# label = name of the dict object (will be title of db table)
def __init__(self, d, label):
#set up the database
cols = [] #make
for k in d.keys():
try:
db = sq.connect('patches.db')
dbc = db.cursor()
except sq.ProgrammingError:
return None
db = sq.connect('patches.db')
dbc = db.cursor()
res = dbc.execute('SELECT name FROM sqlite_master WHERE name="patches"')
if res.fetchone() == None:
res = dbc.execute('CREATE TABLE patches(hash,filename,tag)')
def __init__():
global db
global dbc
try:
db = sq.connect('patches.db')
dbc = db.cursor()
except sq.ProgrammingError:
return None
def get_patches():
try:
res = dbc.execute('SELECT * FROM patches')
return res.fetchall()
except sq.ProgrammingError:
return None
def add_patch(pchID, filename, tag=None):
try:
res = dbc.execute('INSERT INTO patches(hash,filename) VALUES(?,?)', (pchID, filename))
db.commit()
return res
except sq.ProgrammingError:
return None
def remove_patch(pchID):
try:
res = db.execute(f'DELETE FROM patches WHERE hash={pchID}')
db.commit()
return res
except sq.ProgrammingError:
return None
def set_patch_tag(pchID, tag):
try:
res = dbc.execute(f'UPDATE patches SET tag={tag} WHERE hash={pchID}')
db.commit()
return res
except sq.ProgrammingError:
return None
def close():
dbc.close()
db.close()