-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad.py
41 lines (35 loc) · 1.11 KB
/
Load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pandas as pd
import mysql.connector
def load_data():
# Load the transformed data
transformed_data_path = '/home/divithraju/Downloads/salesdata/transformed_region_sales.csv'
df = pd.read_csv(transformed_data_path)
# Establish connection to MySQL
conn = mysql.connector.connect(
host='localhost',
user='divithraju',
password='Divi#567',
database='sales_db'
)
cursor = conn.cursor()
# Create table if it does not exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS region_sales (
region VARCHAR(255),
product_category VARCHAR(255),
month INT,
total_sales FLOAT
)
""")
# Insert data into the table
for _, row in df.iterrows():
cursor.execute("""
INSERT INTO region_sales (region, product_category, month, total_sales)
VALUES (%s, %s, %s, %s)
""", (row['Region'], row['Product Category'], row['Month'], row['Sales Amount']))
conn.commit()
cursor.close()
conn.close()
print("Data loading completed successfully.")
if __name__ == "__main__":
load_data()