-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
49 lines (41 loc) · 1.58 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
#!/usr/bin/env python3
import sqlite3 as sqlite
class Database():
def __init__(self, dbfile):
self.conn = sqlite.connect(dbfile)
def close(self):
self.conn.close()
self.conn = None
def initialise(self):
cur = self.conn.cursor()
cur.execute("CREATE TABLE Tags(Tag BLOB UNIQUE, Name TEXT, Comment TEXT)")
self.conn.commit()
def lookup(self, tag):
cur = self.conn.cursor()
cur.execute("SELECT Name, Comment FROM Tags WHERE Tag = x'"+tag.hex()+"'")
res = cur.fetchone()
if res is None:
raise ValueError("tag not found")
return res
def update(self, tag, name, comment):
t = (name, comment)
cur = self.conn.cursor()
# seems you can't use variables in a where clause...
cur.execute("UPDATE Tags SET Name=?, Comment=? WHERE Tag=x'"+tag.hex()+"'", t)
self.conn.commit()
if cur.rowcount != 1:
raise Exception("tag update failed - does it exist?")
def delete(self, tag):
cur = self.conn.cursor()
# seems you can't use variables in a where clause...
cur.execute("DELETE FROM Tags WHERE Tag=x'"+tag.hex()+"'")
self.conn.commit()
if cur.rowcount != 1:
raise Exception("tag update failed - does it exist?")
def insert(self, tag, name, comment):
t = (name, comment)
cur = self.conn.cursor()
cur.execute("INSERT INTO Tags VALUES(x'"+tag.hex()+"', ?, ?)", t)
self.conn.commit()
if cur.rowcount != 1:
raise Exception("tag insert failed")