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

Populate Database Script

The document provides a Python script for populating an SQLite database from CSV spreadsheets. It defines functions to insert product data from one spreadsheet and shipment data from two others, merging them based on shipping IDs. The script connects to the database, executes the insertions, and closes the connection.

Uploaded by

shivasharma6497
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)
6 views2 pages

Populate Database Script

The document provides a Python script for populating an SQLite database from CSV spreadsheets. It defines functions to insert product data from one spreadsheet and shipment data from two others, merging them based on shipping IDs. The script connects to the database, executes the insertions, and closes the connection.

Uploaded by

shivasharma6497
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

Populate SQLite Database from Spreadsheets

import sqlite3
import pandas as pd

# Paths to spreadsheets and database


DB_PATH = "[Link]"
SPREADSHEET0 = "[Link]"
SPREADSHEET1 = "[Link]"
SPREADSHEET2 = "[Link]"

def insert_spreadsheet0(conn):
df = pd.read_csv(SPREADSHEET0)
cursor = [Link]()
for _, row in [Link]():
[Link]("""
INSERT INTO Products (product_name, manufacturer, category, attribute1,
attribute2)
VALUES (?, ?, ?, ?, ?)
""", (row['name'], row['manufacturer'], row['category'], row['attr1'],
row['attr2']))
[Link]()

def insert_shipments(conn):
df_products = pd.read_csv(SPREADSHEET1)
df_shipments = pd.read_csv(SPREADSHEET2)

cursor = [Link]()

# Merge shipment info with products


merged = df_products.merge(df_shipments, on="shipping_id")

for shipping_id, group in [Link]("shipping_id"):


origin = group['origin'].iloc[0]
destination = group['destination'].iloc[0]

# Insert shipment record


[Link]("""
INSERT INTO Shipments (shipping_id, origin, destination)
VALUES (?, ?, ?)
""", (shipping_id, origin, destination))

# Insert products for this shipment


for _, row in [Link]():
[Link]("""
INSERT INTO ShipmentProducts (shipping_id, product_name, quantity)
VALUES (?, ?, ?)
""", (shipping_id, row['product_name'], row['quantity']))

[Link]()

def main():
conn = [Link](DB_PATH)
insert_spreadsheet0(conn)
insert_shipments(conn)
[Link]()

if __name__ == "__main__":
main()

You might also like