pandas.to_sql with new columns to existing table, add automatically new columns?

前端 未结 2 1490
天涯浪人
天涯浪人 2021-02-08 05:01

I want to write a dataframe to an existing sqlite (or mysql) table and sometimes the dataframe will contain a new column that is not yet present in the database. What do I need

2条回答
  •  既然无缘
    2021-02-08 05:15

    Here is my solution using mySQL and sqlalchemy. The basic idea is that if possible I would like to append to the SQL database instead of re-writing the whole thing, but if there is a new column then I can combine the data in Pandas and then overwrite the existing database.

    import pymysql
    from sqlalchemy import create_engine
    import pandas as pd
    cnx = create_engine('mysql+pymysql://username:password@hostname/database_name')
    try:
        #this will fail if there is a new column
        df.to_sql(name='sql_table', con=cnx, if_exists = 'append', index=False)
    except:
        data = pd.read_sql('SELECT * FROM sql_table', cnx)
        df2 = pd.concat([data,df])
        df2.to_sql(name='sql_table', con=cnx, if_exists = 'replace', index=False)
    

提交回复
热议问题