forked from Haider8/tmessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
33 lines (23 loc) · 1.01 KB
/
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
"""Handles the connection to the SQLite database as well as DB interaction"""
from datetime import datetime
from peewee import CharField, DateTimeField, Model, SqliteDatabase
MESSAGES_DB = SqliteDatabase('message_store.sqlite')
class Message(Model):
"""Message table - keeps track of message sent and received"""
sender = CharField()
message = CharField()
timestamp = DateTimeField()
class Meta: # pylint: disable=missing-class-docstring,too-few-public-methods
database = MESSAGES_DB
@MESSAGES_DB
def grab_messages(user):
"""Grab messages from the user"""
cursor = MESSAGES_DB.execute_sql("SELECT sender, message FROM message where sender LIKE \'" + user +'\'')
for row in cursor.fetchall():
print(row[0] + " : " + row[1])
@MESSAGES_DB
def store_messages(user, raw_msg):
"""Store a message sent by the indicated user in the database"""
time = datetime.now()
Message.create(sender=user, message=raw_msg, timestamp=time)
MESSAGES_DB.create_tables([Message])