问题描述
有时候表需要覆盖,但是不能drop,只能truncate
代码
# insert_rows = pd.io.sql.to_sql(df, target_table_name, db_client.conn, if_exists='replace', index=False,
# dtype={col_name: TEXT for col_name in df})
with db_client.engine.connect() as conn:
conn.execute("TRUNCATE TABLE " + target_table_name)
insert_rows = df.to_sql(name=target_table_name, con=db_client.conn_string, if_exists='append', index=False,
dtype={col_name: TEXT for col_name in df})
参考
【1】How can I truncate a table using pandas?
【2】pandas.DataFrame.to_sql