利用sqlalchemy读取数据库 和pandas的Dataframe对象 互相生成

时间:2022-06-26 07:21:00
#导入pandas
import pandas as pd
import numpy as np #导入SqlAlchemy
from sqlalchemy import create_engine if __name__ == "__main__":
#建立数据库引擎
engine = create_engine('mysql+pymysql://root:mysql@localhost:3306/mymac')
#写一条sql
sql = 'select id,name,age,gender from student'
#建立dataframe
df = pd.read_sql_query(sql,engine)
print(df)
#将dataframe写入数据表,表名,数据引擎,索引是否入库
# df.to_sql('student_copy',engine,index=False)