Добавил:
t.me Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
11
Добавлен:
24.10.2023
Размер:
927 б
Скачать
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()
Соседние файлы в папке Python