2
使用SQLite,我想一個自動增量ID列添加到了以前沒有ID的現有表:的自增ID列到現有的表添加使用SQLite
import sqlite3
db = sqlite3.connect(':memory:')
c = db.cursor()
c.execute('create table events (int weight, text str)')
c.execute('insert into events values(?, ?)', (1371, 'Test1'))
c.execute('insert into events values(?, ?)', (223, 'Test2'))
c.execute('select * from events'); print c.fetchall()
# [(1371, u'Test1'), (223, u'Test2')]
# add an autoincrementing ID to existing table
c.execute('alter table events add id int not null auto_increment primary key')
如何做正確?我有這樣的錯誤:
sqlite3.OperationalError: near "auto_increment": syntax error
如果具有大量行(如my> 1 mln)的表使用'VACUUM'語句來擺脫緩存。 –