· 6 years ago · Jul 24, 2019, 08:38 PM
1import sqlite3
2
3
4class DBHelper:
5
6 def __init__(self, dbname='BookDonkey'):
7 self.dbname = dbname
8 self.connection = sqlite3.connect(dbname)
9
10 def setup(self):
11 text = 'CREATE TABLE IF NOT EXISTS items (description text)'
12 self.connection.execute(text)
13 self.connection.commit()
14
15 def add_item(self, item_text):
16 text = 'INSERT INTO items (description) VALUES (?)'
17 args = (item_text,)
18 self.connection.execute(text, args)
19 self.connection.commit()
20
21 def delete_item(self, item_text):
22 text = 'DELETE FROM items WHERE description = (?)'
23 args = (item_text,)
24 self.connection.execute(text, args)
25 self.connection.commit()
26
27 def get_items(self):
28 text = 'SELECT description FROM items'
29 return [x[0] for x in self.connection.execute(text)]