-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles_57_4_telegramBot_sql_data.py
47 lines (38 loc) · 1.3 KB
/
les_57_4_telegramBot_sql_data.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
import random
import psycopg2
import os
db_params = {
'dbname': os.getenv('POSTGRES_DB', 'postgres'),
'user': os.getenv('POSTGRES_USER', 'admin'),
'password': os.getenv('POSTGRES_PASSWORD', 'root'),
'host': os.getenv('POSTGRES_HOST', 'localhost'),
'port': os.getenv('POSTGRES_PORT', '5432')
}
try:
conn = psycopg2.connect(**db_params)
print("Подключение установлено успешно!")
except psycopg2.Error as e:
print(f"Ошибка подключения: {e}")
exit(1)
cur = conn.cursor()
try:
cur.execute("""
CREATE TABLE IF NOT EXISTS "USER_ADMIN" (
id INTEGER,
username TEXT,
first_name TEXT,
block INTEGER
)
""")
print("Таблица создана успешно!")
except psycopg2.Error as e:
print(f"Ошибка выполнения запроса: {e}")
def user_add(user_id, username, first_name):
check_user = cur.execute("""SELECT * FROM "USER_ADMIN" WHERE id != %s""", (user_id,))
if check_user.fetchone() is None:
cur.execute(f"INSERT INTO \"USER_ADMIN\" VALUES ({user_id},{username},{first_name},0)", (user_id,))
conn.commit()
conn.commit()
cur.close()
conn.close()
print("Соединение с базой данных закрыто.")