-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdb.py
More file actions
27 lines (19 loc) · 792 Bytes
/
db.py
File metadata and controls
27 lines (19 loc) · 792 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
# For convenience sake we won't use SQLAlchemy, but instead use sqlite
import sqlite3
from datetime import datetime
def init():
db = sqlite3.connect('data.db')
# Create table to store comments in
db.cursor().execute('CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY, comment TEXT, date TEXT)')
return db
def add(comment):
db = init()
db.cursor().execute('INSERT INTO comments (comment, date) VALUES (?, ?)', (comment, datetime.utcnow().strftime('%B %d %Y - %H:%M')))
db.commit()
def get(query=None):
db = init()
results = []
for(comment, date) in db.cursor().execute('SELECT comment, date FROM comments').fetchall():
if query is None or query in comment:
results.append((comment, date))
return results