Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions homework-1/create_tables.sql
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
-- SQL-команды для создания таблиц
Create TABLE employees
(
employee_id int PRIMARY KEY,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
title varchar(50) NOT NULL,
birth_date date,
notes text
);

Create TABLE customers
(
customer_id varchar(5) PRIMARY KEY,
company_name varchar(100),
contact_name varchar(100)
);
Create TABLE orders
(
order_id int PRIMARY KEY,
customer_id varchar REFERENCES customers(customer_id) NOT NULL,
employee_id int REFERENCES employees(employee_id) NOT NULL,
order_date date,
ship_city varchar(50),
UNIQUE(customer_id)
)
79 changes: 79 additions & 0 deletions homework-1/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
"""Скрипт для заполнения данными таблиц в БД Postgres."""
import psycopg2
import os
import csv



create_tables = "create_tables.sql" #Should work but for some reason does not open for me
#The code is correnct as far as I'm concerned
employees = os.path.abspath('north_data/employees_data.csv')
orders = os.path.abspath('north_data/orders_data.csv')
customers = os.path.abspath('north_data/customers_data.csv')


#connection
conn = psycopg2.connect(
host='localhost',
database='north',
user='postgres',
password='19182521'
)

def main():
try:
with conn:
with conn.cursor() as cursor:
cursor.execute(open(create_tables, 'r').read())
#enter employee data
with open(employees, 'r', encoding='utf8') as file:
info = csv.reader(file, delimiter=",")
count = 0
for row in info:
if count == 0:
count += 1
continue
else:
cursor.execute("INSERT INTO employees VALUES (%s, %s, %s, %s, %s, %s)",
(int(row[0]), row[1], row[2], row[3], row[4], row[5]))

# enter customer data
with open(customers, 'r', encoding='utf8') as file:
info = csv.reader(file, delimiter=",")
count = 0
for row in info:
if count == 0:
count += 1
continue
else:
try:
cursor.execute("INSERT INTO customers VALUES (%s, %s, %s)",
(row[0], row[1], row[2]))
conn.commit()
except psycopg2.DatabaseError:
conn.rollback()
continue

# enter order data
with open(orders, 'r', encoding='utf8') as file:
info = csv.reader(file, delimiter=",")
count = 0
for row in info:
if count == 0:
count += 1
continue
else:
try:
cursor.execute("INSERT INTO orders VALUES (%s, %s, %s, %s, %s)",
(int(row[0]), row[1], int(row[2]), row[3], row[4]))
conn.commit()
except psycopg2.DatabaseError:
conn.rollback()
continue

finally:
conn.close()



if __name__ == "__main__":
main()