1

I have DataFrame that is needed to save as a table. I used this one and it works well.

result.to_sql(
    'tbl_name',
    index_label='date',
    dtype={'date': Date},
    con=engine.connect(), if_exists='append', index = True
)

The difficult problem is that I can not find a way to set Primary Key for the table. I need to define date(Date) and symbol(String) as Primary Key. As it is impossible to set primary key for sqlite3 after creation, I think there must be a way to set primary key when using to_sql, but could not find. Can anybody help me?

Thank you.

Chuck
  • 776
  • 5
  • 18
  • New to SQLite myself, but think you have to create a temporary table (with your primary keys defined), copy the data to the temp table, drop the old table, then rename the temp table. – AlainD Feb 18 '22 at 11:07
  • Maybe. But I want to use create tables dynamically so that it can save the history data properly. – Chuck Feb 18 '22 at 16:46

0 Answers0