2

I would like to "Add primary or foreign key" to table made using to_sql of Python dataframe. Here is some of my Python code.

This causes "sqlite3.OperationalError: no such table: is_year_002620" But I checked is_year_002620 table created.

I have no idea why this isn't working. How can I "add primary key" to table made by to_sql?

for i in range(startrow, endrow):    
    if df is not None:
        table_name = 'is_year_' + code #code is variable of 6 digit number  and its number is about 1,800  
        df.to_sql(table_name, conn, if_exists='append', index=False)
        conn.cursor().execute("""ALTER TABLE" %s "ADD PRIMARY KEY (code)""" %(table_name))
trigger
  • 91
  • 1
  • 2
  • 6
  • Possible duplicate of [SQLite add Primary Key](https://stackoverflow.com/questions/946011/sqlite-add-primary-key) – Igor Oct 28 '17 at 12:21
  • I flagged this question as duplicated, check out this issue, https://stackoverflow.com/questions/946011/sqlite-add-primary-key, – Igor Oct 28 '17 at 12:22
  • Beside it would be wise to install sqlite3 shell client and execute sql queries with which You have problems from there instead of python script, client responses may be more verbose and helpful than. – Igor Oct 28 '17 at 12:23
  • Thanks. I'll try it – trigger Oct 29 '17 at 07:54

0 Answers0