-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLighter.py
More file actions
36 lines (30 loc) · 1.55 KB
/
SQLighter.py
File metadata and controls
36 lines (30 loc) · 1.55 KB
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
import sqlite3
class SQLighter:
def __init__(self, database):
"""Подключаемся к БД и сохраняем курсор соединения"""
self.connection = sqlite3.connect(database, check_same_thread=False)
self.cursor = self.connection.cursor()
self.cursor.execute("""CREATE TABLE IF NOT EXISTS group_student(
id INTEGER PRIMARY KEY,
id_chat INTEGER,
gr TEXT,
subscribe bool
);""")
self.connection.commit()
def get_chats(self, id_chat):
"""Получаем все группы студентов"""
with self.connection:
return self.cursor.execute("SELECT * FROM `group_student` WHERE `id_chat` = ?", (id_chat,)).fetchall()
def get_group(self, id_chat):
"""Получаем все группы студентов"""
with self.connection:
return self.cursor.execute("SELECT gr FROM `group_student` WHERE `id_chat` = ?", (id_chat,)).fetchall()
def chat_exists(self, id_chat):
"""Проверяем, есть ли уже беседа в базе"""
with self.connection:
result = self.cursor.execute('SELECT * FROM `group_student` WHERE `id_chat` = ?', (id_chat,)).fetchall()
return bool(len(result))
def add_chat(self, id_chat, group, subscribe = True):
"""Добавляем новую беседу"""
with self.connection:
return self.cursor.execute("INSERT INTO `group_student` (`id_chat`, `gr`,`subscribe`) VALUES(?,?,?)", (id_chat, group, subscribe))