Skip to content

Commit a2cfc09

Browse files
authored
Python script for data import to MySQL database
1 parent 1f2d5c7 commit a2cfc09

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

mysql_data_import.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Connect and import multiple csv files into MySQL Database
2+
3+
import pandas as pd
4+
from sqlalchemy import create_engine
5+
import os
6+
7+
# Set up engine ('mysql+mysqlconnector://user:password@hostname/dbname)
8+
engine = create_engine('mysql+mysqlconnector://root:***********@localhost/adventureworks2022')
9+
10+
# Path to directory containing csv files
11+
csv_dir = '/Users/jaypaints/Downloads/AdventureWorks2022'
12+
13+
# Exract csv files in the directory into a list
14+
csv_files = [file for file in os.listdir(csv_dir) if file.lower().endswith('.csv')]
15+
16+
# Loop over the csv files, extract file names and use as database table names
17+
for file in csv_files:
18+
table_name = os.path.splitext(file)[0].lower()
19+
csv_path = os.path.join(csv_dir, file)
20+
# Read csv files into pandas DataFrames
21+
df = pd.read_csv(csv_path, encoding = 'latin1')
22+
print(f"--- Data read from csv file into DataFrame {table_name}. ---")
23+
print(df.head())
24+
# Create and query the tables in the database
25+
df.to_sql(name = table_name, con = engine, if_exists = 'replace', index = False)
26+
sql = 'SELECT * FROM ' + table_name
27+
data_db = pd.read_sql_query(sql, engine)
28+
print(f"--- DB table {table_name} created successfully! ---")
29+
print(data_db.head())
30+
31+
print("All csv files imported into separate tables in the adventureworks2022 database.")
32+

0 commit comments

Comments
 (0)