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

#导入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)

 

posted @ 2019-03-10 17:57  TheoldmanPickgarbage  阅读(1580)  评论(0编辑  收藏  举报