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'语句来摆脱缓存。 –