pandas连接mysql数据库
1.安装依赖 "UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other # DBAPI2 objects are not tested. Please consider using SQLAlchemy."
conda install sqlalchemy pymysql
2.导入类库
from sqlalchemy import create_engine
import pandas as pd
3.创建数据库连接字符串
user = 'root'
password = '123456'
host = '127.0.0.1'
port = '3306'
database = 'lrcore_cloud_xxx'
db_url = f'mysql+pymysql://{user}:{password}@{host}:{port}/{database}'
4.创建 SQLAlchemy 引擎
engine = create_engine(db_url)
5.使用 Pandas 读取数据
df = pd.read_sql_table('user', engine)
print(df)
'''
输出结果
id name
0 1 jack
1 2 boom
2 3 lucy
3 4 jack
4 5 boom
5 6 lucy
'''