questions about pandas.to_sql

前端 未结 1 583
借酒劲吻你
借酒劲吻你 2020-12-14 21:31

I have a question about how to save a dataframe to my local mysql.

import MySQLdb
import pandas as pd
conn=MySQLdb.connect(host=\"localhost\",user=\'root\',p         


        
相关标签:
1条回答
  • 2020-12-14 21:52

    Your way is not supported anymore.

    con : SQLAlchemy engine or DBAPI2 connection (legacy mode)

    Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

    flavor : ‘sqlite’, default None

    Deprecated since version 0.19.0: ‘sqlite’ is the only supported option if SQLAlchemy is not used.

    pandas.DataFrame.to_sql

    Try this?

    from sqlalchemy import create_engine
    import pandas as pd
    
    
    engine = create_engine("mysql://root:matt123@localhost/ada")
    con = engine.connect()
    df = pd.DataFrame(['A','B'],columns=['new_tablecol'])
    df.to_sql(name='new_table',con=con,if_exists='append')
    con.close()
    

    Syntax is:

    engine = create_engine("mysql://USER:PASSWORD@HOST/DATABASE")
    

    More information about sqlalchemy can be found here

    0 讨论(0)
提交回复
热议问题