如何将主/外键添加到to_sql生成的表中?

时间:2017-10-28 11:46:08

标签: python sqlite

我想"添加主键或外键"使用Python数据帧的to_sql创建的表。这是我的一些Python代码。

这导致" sqlite3.OperationalError:没有这样的表:is_year_002620" 但我检查了is_year_002620表创建。

我不知道为什么这不起作用。我怎样才能添加主键"到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))

0 个答案:

没有答案