0% found this document useful (0 votes)
17 views2 pages

Walmart Module4

Uploaded by

Vraj Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Walmart Module4

Uploaded by

Vraj Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import sqlite3

import csv

# Open a connection to the SQLite database


conn = sqlite3.connect('shipment_database.db')

# Create a cursor object to execute SQL commands


c = conn.cursor()

# Create a table to store the data


c.execute('''CREATE TABLE csv_0_data
(id INTEGER PRIMARY KEY,
origin_warehouse TEXT,
destination_store TEXT,
product TEXT,
on_time INTEGER,
product_quantity INTEGER,
driver_identifier TEXT)''')

# Read the data from the CSV file and insert it into the database
with open('shipping_data_0.csv', 'r') as f:
# Skip the header row
next(f)
# Loop through each line in the file
for line in f:
# Split the line into fields
fields = line.strip().split(',')
# Insert the data into the table
c.execute('''INSERT INTO civ_data
(origin_warehouse, destination_store, product, on_time, product_quantity, driver_identifier)
VALUES (?, ?, ?, ?, ?, ?)''',
(fields[0], fields[1], fields[2], int(fields[3]), int(fields[4]), fields[5]))

with open('shipping_data_1.csv') as file:


reader = csv.reader(file)
for row in reader:
shipment_id, product, on_time = row
quantity = 1
c.execute('''
INSERT INTO products (shipment_id, product, on_time, quantity)
VALUES (?, ?, ?, ?)
''', (shipment_id, product, on_time, quantity))

# Read data from spreadsheet 2 and update the 'products' table with shipment details
with open('shipping_data_2.csv') as file:
reader = csv.reader(file)
for row in reader:
shipment_id, origin_warehouse, destination_store, driver_identifier = row
c.execute('''
UPDATE products
SET shipment_id=?, quantity=(
SELECT COUNT(*) FROM products WHERE shipment_id=?
)
WHERE shipment_id=?
''', (shipment_id, shipment_id, shipment_id))

# Insert shipment details into the 'shipments' table


with open('shipping_data_2.csv') as file:
reader = csv.reader(file)
for row in reader:
shipment_id, origin_warehouse, destination_store, driver_identifier = row
c.execute('''
INSERT INTO shipments (shipment_id, origin_warehouse, destination_store, driver_identifier)
VALUES (?, ?, ?, ?)
''', (shipment_id, origin_warehouse, destination_store, driver_identifier))

# Commit the changes to the database


conn.commit()

# Close the database connection


conn.close()

You might also like