-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles_55_1_sqlpostgress.py
48 lines (38 loc) · 1.23 KB
/
les_55_1_sqlpostgress.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
from random import random
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()
#cur.execute("""SELECT COUNT(*) FROM "USER" WHERE age>20""")
# total1 = cur.fetchone()[0]
# total2 = cur.fetchall()
# print(total1)
# print(total2)
# cur.execute("""SELECT SUM(age) FROM "USER" """)
# total_first_sum = cur.fetchone()[0]
# cur.execute("""SELECT COUNT(*) FROM "USER" """)
# total_second_sum = cur.fetchone()[0]
# print(total_first_sum/total_second_sum)
cur.execute("""SELECT MAX(age) FROM "USER" """)
total_data = cur.fetchone()[0]
print(total_data)
# users = cur.fetchall()
# for user in users:
# print(f"Пользователь: {user}")
conn.commit()
cur.close()
conn.close()
print("Соединение с базой данных закрыто.")