How to connect pandas to MySQL database
Apr 17, 2023
We can connect pandas to MySQL. It is easy very easy.
Simple import sqlalchemy library. If library not available you need to install sqlalchemy. Enter following command.
pip install SQLAlchemypy
After installed sqlalchemy you can write your code.
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine, event, select, MetaData, Table, and_
engine = create_engine("mysql+pymysql://username:password#@localhost:3306/bs_report?charset=utf8mb4")
mytable = engine.execute("select * from tablename")
df = pd.DataFrame(mytable)
Simple change your database details in URL and you can use any MySQL query.
Thank you