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()