Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework1 #38

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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(100) NOT NULL,
last_name varchar(100) NOT NULL,
title varchar(100) NOT NULL,
birth_date varchar(100) NOT NULL,
notes varchar(500) NOT NULL
);

CREATE TABLE customers
(
customer_id varchar(10) PRIMARY KEY,
company_name varchar(100) NOT NULL,
contact_name varchar(100) NOT NULL
);

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

FILE_CUSTOMERS_DATA = 'north_data/customers_data.csv'
FILE_EMPLOYEES_DATA = 'north_data/employees_data.csv'
FILE_ORDERS_DATA = 'north_data/orders_data.csv'


def insert_data_to_sql(filename, name_table):
with open(filename, 'r', encoding='UTF-8') as file:
data = csv.DictReader(file)
with psycopg2.connect(host="localhost", database="north", user="denis", password="123456") as conn:
with conn.cursor() as curs:
for row in data:
row_data = []
values_designation = []
for key in list(row.keys()):
row_data.append(row[key])
values_designation.append('%s')
curs.execute(f"INSERT INTO {name_table} VALUES ({', '.join(values_designation)})", tuple(row_data))
row_data.clear()
values_designation.clear()
conn.close()


insert_data_to_sql(FILE_CUSTOMERS_DATA, 'customers')
insert_data_to_sql(FILE_EMPLOYEES_DATA, 'employees')
insert_data_to_sql(FILE_ORDERS_DATA, 'orders')
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.poetry]
name = "postgres-homeworks"
version = "0.1.0"
description = ""
authors = ["Denis Prusakov <Denikpr@gmail.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"