diff --git a/database_script_code.pdf b/database_script_code.pdf new file mode 100644 index 00000000..3723ee33 Binary files /dev/null and b/database_script_code.pdf differ diff --git a/pdf.py b/pdf.py new file mode 100644 index 00000000..c79c8fdf --- /dev/null +++ b/pdf.py @@ -0,0 +1,80 @@ +from fpdf import FPDF +import pandas as pd +import sqlite3 + +# Code to be added to the PDF +code_text = """ +import pandas as pd +import sqlite3 + +#----CSV Files paths +csv_file_0 = 'data/shipping_data_0.csv' +csv_file_1 = 'data/shipping_data_1.csv' +csv_file_2 = 'data/shipping_data_2.csv' +database_path = 'shipment_database.db' + +#----Reading CSV files into DataFrames +df0 = pd.read_csv(csv_file_0) +df1 = pd.read_csv(csv_file_1) +df2 = pd.read_csv(csv_file_2) + +#---Printing columns to debug +print("Columns in df1:", df1.columns) +print("Columns in df2:", df2.columns) + +#---Combining data from shipping_data_1.csv and shipping_data_2.csv +df_combined = pd.merge(df1, df2, on='shipment_identifier') + +#---Calculating total quantities for each shipment +df_combined['total_quantity'] = df_combined.groupby('shipment_identifier')['product'].transform('size') + +#---Connecting to SQLite database +conn = sqlite3.connect(database_path) +cursor = conn.cursor() + +#---Creating table shipping_data_1 if not exists +cursor.execute(''' + CREATE TABLE IF NOT EXISTS shipping_data_1 ( + shipment_identifier TEXT, + product TEXT, + on_time TEXT, + origin_warehouse TEXT, + destination_store TEXT, + driver_identifier TEXT, + quantity INTEGER + ) +''') + +#--Inserting data from shipping_data_0.csv +df0.to_sql('shipping_data_0', conn, if_exists='append', index=False) + +#---Inserting combined data into shipping_data_1 table +for index, row in df_combined.iterrows(): + cursor.execute( + # INSERT INTO shipping_data_1 (shipment_identifier, + # product, on_time, origin_warehouse, destination_store, driver_identifier, quantity) + # VALUES (?, ?, ?, ?, ?, ?, ?) + # (row['shipment_identifier'], row['product'], row['on_time'], row['origin_warehouse'], row['destination_store'], row['driver_identifier'], row['total_quantity'])) + +#---Commiting and close connection +conn.commit() +conn.close() + +print("Data inserted successfully!") +""" + +# Creating a PDF object +pdf = FPDF() +pdf.add_page() +pdf.set_auto_page_break(auto=True, margin=15) +pdf.set_font("Courier", size=8) + +# Adding code content to the PDF +for line in code_text.splitlines(): + pdf.cell(200, 5, txt=line, ln=True) + +# Save the PDF file +pdf_file_path = "database_script_code.pdf" +pdf.output(pdf_file_path) + +print(f"PDF created successfully at {pdf_file_path}") \ No newline at end of file diff --git a/script.py b/script.py new file mode 100644 index 00000000..97eed494 --- /dev/null +++ b/script.py @@ -0,0 +1,56 @@ +import pandas as pd +import sqlite3 + +#----CSV Files paths +csv_file_0 = 'data/shipping_data_0.csv' +csv_file_1 = 'data/shipping_data_1.csv' +csv_file_2 = 'data/shipping_data_2.csv' +database_path = 'shipment_database.db' + +#----Reading CSV files into DataFrames +df_main = pd.read_csv(csv_file_0) +df_ship_info = pd.read_csv(csv_file_1) +df_ship_loc = pd.read_csv(csv_file_2) + +#---Printing columns to debug +print("Columns in df_ship_info:", df_ship_info.columns) +print("Columns in df_ship_loc:", df_ship_loc.columns) + +#---Combining data from shipping_data_1.csv and shipping_data_2.csv +df_combined = pd.merge(df_ship_info, df_ship_loc, on='shipment_identifier') + +#---Calculating total quantities for each shipment +df_combined['total_qty'] = df_combined.groupby('shipment_identifier')['product'].transform('size') + +#---Connecting to SQLite database +connection = sqlite3.connect(database_path) +cur = connection.cursor() + +#---Creating table shipping_data_1 if not exists +cur.execute(''' + CREATE TABLE IF NOT EXISTS shipping_data_1 ( + shipment_identifier TEXT, + product TEXT, + on_time TEXT, + origin_warehouse TEXT, + destination_store TEXT, + driver_identifier TEXT, + quantity INTEGER + ) +''') + +#--Inserting data from shipping_data_0.csv +df_main.to_sql('shipping_data_0', connection, if_exists='append', index=False) + +#---Inserting combined data into shipping_data_1 table +for idx, record in df_combined.iterrows(): + cur.execute(""" + INSERT INTO shipping_data_1 (shipment_identifier, product, on_time, origin_warehouse, destination_store, driver_identifier, quantity) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, (record['shipment_identifier'], record['product'], record['on_time'], record['origin_warehouse'], record['destination_store'], record['driver_identifier'], record['total_qty'])) + +#---Commiting and close connection +connection.commit() +connection.close() + +print("Data inserted successfully!") diff --git a/shipment_database.db b/shipment_database.db index fd46f283..11e3396b 100644 Binary files a/shipment_database.db and b/shipment_database.db differ