-
Notifications
You must be signed in to change notification settings - Fork 1
/
journalist_db.py
44 lines (36 loc) · 1.34 KB
/
journalist_db.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
35
36
37
38
39
40
41
42
43
44
import os
import sqlite3
class JournalistDatabase():
def __init__(self, path):
self.path = path
self.is_valid = os.path.isfile(self.path)
self.con = sqlite3.connect(self.path)
if self.is_valid == False:
try:
self.create()
except sqlite3.OperationalError:
pass
def __del__(self):
self.con.close()
def create(self):
cur = self.con.cursor()
cur.execute("CREATE TABLE messages (sender TEXT NOT NULL, timestamp TEXT NOT NULL, content TEXT NOT NULL);")
self.con.commit()
def select_messages(self, sender):
cur = self.con.cursor()
cur.execute("SELECT timestamp, content FROM messages WHERE sender = ? ORDER BY timestamp;", (sender,))
rows = cur.fetchall()
messages = rows if len(rows) > 0 else []
self.con.commit()
return messages
def insert_message(self, sender, timestamp, content):
cur = self.con.cursor()
cur.execute("INSERT INTO messages (sender, timestamp, content) VALUES (?,?,?);", (sender, timestamp, content))
self.con.commit()
return cur.lastrowid
"""
def delete_message(self, message_id):
cur = self.con.cursor()
cur.execute("DELETE FROM messages WHERE message_id = ?;", message_id)
self.con.commit()
"""