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

Script #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import csv
import sqlite3

def create_tables(cursor):
cursor.execute("""
CREATE TABLE IF NOT EXISTS shipping_data_0 (
origin_warehouse TEXT,
destination_store TEXT,
product TEXT,
on_time TEXT,
product_quantity INTEGER,
driver_identifier TEXT
)
""")

cursor.execute("""
CREATE TABLE IF NOT EXISTS shipping_data_1 (
shipment_identifier TEXT,
product TEXT,
on_time TEXT,
origin_warehouse TEXT,
destination_store TEXT
)
""")

def insert_shipping_data_0(cursor):
with open('data/shipping_data_0.csv', 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader)
for row in csv_reader:
origin_warehouse, destination_store, product, on_time, product_quantity, driver_identifier = row
cursor.execute("INSERT INTO shipping_data_0 (origin_warehouse, destination_store, product, on_time, product_quantity, driver_identifier) VALUES (?, ?, ?, ?, ?, ?)",
(origin_warehouse, destination_store, product, on_time, product_quantity, driver_identifier))


# combining each row based on its shipping identifier, finding the quantity of goods in the shipment, and adding a new row to the database for each product in the shipment.

def insert_shipping_data_2(cursor):
# Read and process shipping data from 'shipping_data_2.csv'
with open('data/shipping_data_2.csv', 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader) # Skip the header row
shipping_data_2_rows = [row for row in csv_reader]

# Read and process shipping data from 'shipping_data_1.csv'
with open('data/shipping_data_1.csv', 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader) # Skip the header row

# Loop through rows in 'shipping_data_1.csv'
for row in csv_reader:
shipment_identifier, product, on_time = row

# Find matching rows in 'shipping_data_2_rows' based on 'shipment_identifier'
matching_rows = [r for r in shipping_data_2_rows if r[0] == shipment_identifier]

# If a matching row is found in 'shipping_data_2_rows'
if matching_rows:
origin_warehouse, destination_store, driver_identifier = matching_rows[0][1], matching_rows[0][2], matching_rows[0][3]

# Execute an SQL INSERT statement to insert data into the 'shipping_data_1' table
cursor.execute("INSERT INTO shipping_data_1 (shipment_identifier, product, on_time, origin_warehouse, destination_store) VALUES (?, ?, ?, ?, ?)",
(shipment_identifier, product, on_time, origin_warehouse, destination_store))

if __name__ == "__main__":
# Establishing connection to sqlite if the script is being run as the main program
conn = sqlite3.connect('shipment_database.db')
cursor = conn.cursor()

create_tables(cursor) # Creating required table

insert_shipping_data_0(cursor)
insert_shipping_data_2(cursor)

# Commit changes and close the database connection
conn.commit()
conn.close()