-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_sql.py
71 lines (58 loc) · 2.24 KB
/
csv_to_sql.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pandas as pd
import mysql.connector
import os
# List of CSV files and their corresponding table names
csv_files = [
('customers.csv', 'customers'),
('orders.csv', 'orders'),
('sales.csv', 'sales'),
('products.csv', 'products'),
('delivery.csv', 'delivery'),
('payments.csv', 'payments') # Added payments.csv for specific handling
]
# Connect to the MySQL database
conn = mysql.connector.connect(
host='your_host',
user='your_username',
password='your_password',
database='your_database'
)
cursor = conn.cursor()
# Folder containing the CSV files
folder_path = 'path_to_your_folder'
def get_sql_type(dtype):
if pd.api.types.is_integer_dtype(dtype):
return 'INT'
elif pd.api.types.is_float_dtype(dtype):
return 'FLOAT'
elif pd.api.types.is_bool_dtype(dtype):
return 'BOOLEAN'
elif pd.api.types.is_datetime64_any_dtype(dtype):
return 'DATETIME'
else:
return 'TEXT'
for csv_file, table_name in csv_files:
file_path = os.path.join(folder_path, csv_file)
# Read the CSV file into a pandas DataFrame
df = pd.read_csv(file_path)
# Replace NaN with None to handle SQL NULL
df = df.where(pd.notnull(df), None)
# Debugging: Check for NaN values
print(f"Processing {csv_file}")
print(f"NaN values before replacement:\n{df.isnull().sum()}\n")
# Clean column names
df.columns = [col.replace(' ', '_').replace('-', '_').replace('.', '_') for col in df.columns]
# Generate the CREATE TABLE statement with appropriate data types
columns = ', '.join([f'`{col}` {get_sql_type(df[col].dtype)}' for col in df.columns])
create_table_query = f'CREATE TABLE IF NOT EXISTS `{table_name}` ({columns})'
cursor.execute(create_table_query)
# Insert DataFrame data into the MySQL table
for _, row in df.iterrows():
# Convert row to tuple and handle NaN/None explicitly
values = tuple(None if pd.isna(x) else x for x in row)
sql = f"INSERT INTO `{table_name}` ({', '.join(['`' + col + '`' for col in df.columns])}) VALUES ({', '.join(['%s'] * len(row))})"
cursor.execute(sql, values)
# Commit the transaction for the current CSV file
conn.commit()
# Close the connection
conn.close()