-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
169 lines (117 loc) · 3.89 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import sqlite3
def connect_to_database():
"""
Подключение к базе данных residents.db.
Returns:
sqlite3.Connection: Объект соединения.
"""
return sqlite3.connect("residents.db")
def create_table():
"""
Создание таблицы users.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
last_name TEXT NOT NULL,
first_name TEXT NOT NULL,
block_number INTEGER NOT NULL,
room_number INTEGER NOT NULL,
admin INTEGER DEFAULT 0
);
""")
connection.commit()
connection.close()
def add_user(id, last_name, first_name, block_number, room_number):
"""
Добавление пользователя в таблицу.
Args:
last_name (str): Фамилия.
first_name (str): Имя.
block_number (int): Номер блока.
room_number (int): Номер комнаты.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
INSERT INTO users (id, last_name, first_name, block_number, room_number)
VALUES (?, ?, ?, ?, ?)
""", (id, last_name, first_name, block_number, room_number))
connection.commit()
connection.close()
def check_room_exists(block_number, room_number):
"""
Проверка, существует ли комната.
Args:
block_number (int): Номер блока.
room_number (int): Номер комнаты.
Returns:
bool: True, если комната существует, False - otherwise.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
SELECT COUNT(*) FROM users
WHERE block_number = ? AND room_number = ?
""", (block_number, room_number))
room_exists = cursor.fetchone()[0]
connection.close()
return bool(room_exists)
def get_residents(block_number, room_number):
"""
Получение списка жильцов.
Args:
block_number (int): Номер блока.
room_number (int): Номер комнаты.
Returns:
list: Список жильцов.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
SELECT id, last_name, first_name FROM users
WHERE block_number = ? AND room_number = ?
""", (block_number, room_number))
residents = cursor.fetchall()
connection.close()
return residents
def check_if_user_registered(user_id):
"""
Проверка регистрации пользователя.
Args:
user_id (int): ID пользователя.
Returns:
bool: True, если пользователь зарегистрирован, False - otherwise.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
SELECT COUNT(*) FROM users
WHERE id = ?
""", (user_id,))
result = cursor.fetchone()[0]
connection.close()
return bool(result)
def get_name_from_db(user_id):
"""
Получение имени пользователя.
Args:
user_id (int): ID пользователя.
Returns:
str: Имя пользователя.
"""
connection = connect_to_database()
cursor = connection.cursor()
cursor.execute("""
SELECT first_name, last_name FROM users
WHERE id = ?
""", (user_id,))
result = cursor.fetchone()
connection.close()
if result:
first_name, last_name = result
return f"{first_name} {last_name}"
else:
return None