Using sqlalchemy to upload csv files into MySQL Database
After activating your python environment where you want to code, Copy the following and press enter.
pip install sqlalchemy
pip install mysqlclient
Then go ahead and download a csv file. Assuming you have pandas already installed, copy and paste the following code.
from sqlalchemy import create_engine as ce
mysqlengine= ce("mysql://root:root@127.0.0.1:3306/nhs_trust")
import pandas as pd
df = pd.read_csv("E:\python\datacamp_basics\lalaland.csv")
df.to_sql('thulaland', mysqlengine, if_exists='replace', index= False)
Code Explanation
Importing the necessary modules:
from sqlalchemy import create_engine as ce import pandas as pd
This code imports the
create_engine
function from SQLAlchemy asce
and thepandas
library aspd
. Thecreate_engine
function is used to create a SQLAlchemy engine, andpandas
is used to work with data frames, including reading and writing CSV files.Creating a SQLAlchemy engine:
mysqlengine = ce("mysql://root:root@127.0.0.1:3306/nhs_trust")
This code creates a SQLAlchemy engine called
mysqlengine
using thecreate_engine
function. The engine is configured to connect to a MySQL database with the following connection URL:mysql://root:root@127.0.0.1:3306/nhs_trust
mysql
specifies the dialect for MySQL.root:root
represents the username and password for the database connection. In my caseroot
was username androot
was password.127.0.0.1:3306
specifies the host and port for the MySQL server.nhs_trust
is the name of the database.
Reading a CSV file into a pandas DataFrame:
df = pd.read_csv("E:\python\datacamp_basics\lalaland.csv")
This code uses the
read_csv
function frompandas
to read the CSV file located at"E:\python\datacamp_basics\lalaland.csv"
. The contents of the CSV file are loaded into a pandas DataFrame nameddf
.Uploading the DataFrame to a MySQL table:
df.to_sql('thulaland', mysqlengine, if_exists='replace', index=False)
This code uploads the data from the pandas DataFrame
df
to a MySQL table named'thulaland'
using theto_sql
method. The parameters passed toto_sql
are:'thulaland'
specifies the name of the destination table.mysqlengine
is the SQLAlchemy engine to be used for the database connection.if_exists='replace'
indicates that if the table already exists, it should be replaced.index=False
specifies that the DataFrame's index should not be included in the table.
The to_sql
method converts the DataFrame into a SQL insert statement and executes it to upload the data to the specified table in the MySQL database.
Make sure to adjust the file path and database connection details according to your specific setup.