-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles_54_1_sqlpostgressBegin.py
64 lines (58 loc) · 2.06 KB
/
les_54_1_sqlpostgressBegin.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
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()
try:
cur.execute("""
CREATE TABLE IF NOT EXISTS "USER" (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL,
age INTEGER
)
""")
print("Таблица создана успешно!")
except psycopg2.Error as e:
print(f"Ошибка выполнения запроса: {e}")
# for i in range(30):
# cur.execute("""
# insert into "USER" (username, email, age) values (%s, %s, %s)
# """, (f"newuser_{i}", f"sdfsd_{i}@mail.ru", str(random.randint(18, 50))))
for i in range(30):
username = f"newuser_{i}"
email = f"sdfsd_{i}@mail.ru"
age = random.randint(18, 50)
cur.execute("""
INSERT INTO "USER" (username, email, age)
SELECT %s, %s, %s
WHERE NOT EXISTS (
SELECT 1 FROM "USER" WHERE username = %s
)
""", (username, email, age, username))
cur.execute("""UPDATE "USER" SET age = %s WHERE username = %s""", (28, "newuser_12"))
cur.execute("""DELETE FROM "USER" WHERE username=%s""", ("newuser_14",))
cur.execute("""SELECT * FROM "USER" """)
cur.execute("""SELECT username, age FROM "USER" WHERE age > 20""")
cur.execute("""SELECT username, age FROM "USER" GROUP BY age, username""")
cur.execute("""SELECT username, age FROM "USER" ORDER BY age ASC""")
users = cur.fetchall()
for user in users:
print(f"Пользователь: {user}")
conn.commit()
cur.close()
conn.close()
print("Соединение с базой данных закрыто.")