forked from yoojungjoe3/4300-Flask-Template-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_sql.py
More file actions
27 lines (20 loc) · 805 Bytes
/
Copy pathcsv_to_sql.py
File metadata and controls
27 lines (20 loc) · 805 Bytes
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
import pandas as pd
# Define filenames
csv_file = "DataSet.csv" # Use cleaned data
sql_file = "init.sql"
table_name = "quillquestdb" # Your MySQL table name
# Load CSV
df = pd.read_csv(csv_file)
# Remove leading/trailing spaces and convert column names to lowercase
df.columns = df.columns.str.strip()
# Ensure unique rows before generating SQL
df = df.drop_duplicates()
# Generate INSERT statements
insert_sql = ""
for _, row in df.iterrows():
values = "', '".join(map(str, row.tolist())) # Convert row values to strings
insert_sql += f"INSERT INTO `{table_name}` ({', '.join(df.columns)}) VALUES ('{values}');\n"
# Save to SQL file
with open(sql_file, "w", encoding="utf-8") as f:
f.write(insert_sql)
print(f"SQL INSERT file '{sql_file}' created successfully with no duplicates.")