-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdb.py
More file actions
27 lines (21 loc) · 748 Bytes
/
db.py
File metadata and controls
27 lines (21 loc) · 748 Bytes
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
import sqlite3
def connect_db():
db = sqlite3.connect('database.db')
db.cursor().execute('CREATE TABLE IF NOT EXISTS comments '
'(id INTEGER PRIMARY KEY, '
'comment TEXT)')
db.commit()
return db
def add_comment(comment):
db = connect_db()
db.cursor().execute('INSERT INTO comments (comment) '
'VALUES (?)', (comment,))
db.commit()
def get_comments(search_query=None):
db = connect_db()
results = []
get_all_query = 'SELECT comment FROM comments'
for (comment,) in db.cursor().execute(get_all_query).fetchall():
if search_query is None or search_query in comment:
results.append(comment)
return results