【发布时间】:2014-09-22 02:11:09
【问题描述】:
在 Python 3.4.1 中,为什么sqlite3 不将自动递增的 ID 插入到下面程序的表中?根据the SQLite documentation,整数主键列应该自动递增,我可以从 cur.lastrowid 返回一个有效整数中看到,但相同的值没有插入到表中(而是变为 NULL)。
import sqlite3
with sqlite3.connect(':memory:') as conn:
cur = conn.cursor()
# Note that column 'id' is an integer primary key
cur.execute('create table test (id int primary key , name text)')
cur.execute('insert into test (name) values (?)', ('Test',))
last_id = cur.lastrowid
assert last_id is not None
id_, = cur.execute('select id from test').fetchone()
assert id_ == last_id, '{} != {}'.format(id_, last_id)
【问题讨论】:
标签: python python-3.x sqlite