-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
74 lines (64 loc) · 2.12 KB
/
model.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sqlite3
from datetime import datetime
class Models:
def __init__(self):
self.db = sqlite3.connect('database.db', check_same_thread=False)
self.cursor = self.db.cursor()
self.create_table()
def create_table(self):
"""Creates table if not exists"""
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS subscriptions (
chat_id TEXT PRIMARY KEY NOT NULL,
first_name TEXT DEFAULT "",
last_name TEXT DEFAULT "",
subsdate DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
chat_id TEXT NOT NULL,
first_name TEXT DEFAULT "",
last_name TEXT DEFAULT "",
message TEXT DEFAULT "",
message_date DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
self.db.commit()
def add_user(self, chat_id, first_name, last_name):
"""Adds people to database"""
self.cursor.execute(
'''
INSERT INTO subscriptions (chat_id, first_name, last_name) VALUES (?, ?, ?)
''', (chat_id, first_name, last_name))
self.db.commit()
def delete_person(self, chat_id):
"""Deletes people from database"""
self.cursor.execute(
'''
DELETE FROM subscriptions WHERE chat_id = ?
''', (chat_id, ))
self.db.commit()
def check_person(self, chat_id):
"""Checks if people exists"""
self.cursor.execute(
'''
SELECT * FROM subscriptions WHERE chat_id = ?
''', (chat_id, ))
return self.cursor.fetchone()
def check_all(self):
self.cursor.execute('''
SELECT * FROM subscriptions
''')
return self.cursor.fetchall()
def add_message(self, chat_id, first_name, last_name, message):
self.cursor.execute(
"INSERT INTO messages (chat_id, first_name, last_name,message) VALUES (?, ?, ?, ?)",
(chat_id, first_name, last_name, message))
self.db.commit()
def get_all_messages(self):
self.cursor.execute('''
SELECT * FROM messages
''')
return self.cursor.fetchall()