-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbhelper.py
32 lines (27 loc) · 1.05 KB
/
dbhelper.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
import sqlite3
class DBHelper:
def __init__(self, dbname="todo.sqlite"):
self.dbname = dbname
self.conn = sqlite3.connect(dbname)
def setup(self):
tblstmt = "CREATE TABLE IF NOT EXISTS items (description text, owner text)"
itemidx = "CREATE INDEX IF NOT EXISTS itemIndex ON items (description ASC)"
ownidx = "CREATE INDEX IF NOT EXISTS ownIndex ON items (owner ASC)"
self.conn.execute(tblstmt)
self.conn.execute(itemidx)
self.conn.execute(ownidx)
self.conn.commit()
def add_item(self, item_text, owner):
stmt = "INSERT INTO items (description, owner) VALUES (?, ?)"
args = (item_text, owner)
self.conn.execute(stmt, args)
self.conn.commit()
def delete_item(self, item_text, owner):
stmt = "DELETE FROM items WHERE description = (?) AND owner = (?)"
args = (item_text, owner )
self.conn.execute(stmt, args)
self.conn.commit()
def get_items(self, owner):
stmt = "SELECT description FROM items WHERE owner = (?)"
args = (owner, )
return [x[0] for x in self.conn.execute(stmt, args)]