forked from hastagAB/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbhelper.py
executable file
·35 lines (27 loc) · 1.19 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
33
34
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, due_date date ,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, due_date, owner):
stmt = "INSERT INTO items (description, due_date ,owner) VALUES (?, ? ,?)"
args = (item_text, due_date ,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,due_date FROM items WHERE owner = (?)"
args = (owner, )
return [x for x in self.conn.execute(stmt, args)]