
Добавил:
KaFaka
t.me
Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:4 курс 1 семестр / Лабораторные / Python / LR7_4
.py import psycopg2
import random
connection = psycopg2.connect(database="students",
user="postgres",
password="123",
host="127.0.0.1",
port="5432")
cursor = connection.cursor()
# Add location_id column to employees table
cursor.execute("ALTER TABLE public.employees ADD COLUMN location_id int REFERENCES public.locations (location_id)")
# Fetch all location_id from locations table
cursor.execute("SELECT location_id FROM public.locations")
location_ids = cursor.fetchall()
location_ids = [location_id[0] for location_id in location_ids]
# Update location_id for each employee randomly
for i in range(1, 54):
cursor.execute(f"UPDATE public.employees SET location_id={random.choice(location_ids)} WHERE employee_id={i}")
connection.commit()
cursor.close()
connection.close()
import random
connection = psycopg2.connect(database="students",
user="postgres",
password="123",
host="127.0.0.1",
port="5432")
cursor = connection.cursor()
# Add location_id column to employees table
cursor.execute("ALTER TABLE public.employees ADD COLUMN location_id int REFERENCES public.locations (location_id)")
# Fetch all location_id from locations table
cursor.execute("SELECT location_id FROM public.locations")
location_ids = cursor.fetchall()
location_ids = [location_id[0] for location_id in location_ids]
# Update location_id for each employee randomly
for i in range(1, 54):
cursor.execute(f"UPDATE public.employees SET location_id={random.choice(location_ids)} WHERE employee_id={i}")
connection.commit()
cursor.close()
connection.close()
Соседние файлы в папке Python