微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与python的sqlite问题中的AUTO_INCREMENT

我正在使用python 2.5的sqlite.我用下面的语法得到一个sqlite错误.我环顾四周,在这页面http://www.sqlite.org/syntaxdiagrams.html#column-constraint上看到了AUTOINCREMENT,但这也无效.没有AUTO_INCREMENT我可以创建表.

An error occurred: near "AUTO_INCREMENT": Syntax error 
CREATE TABLE fileInfo
(
fileId int NOT NULL AUTO_INCREMENT,
name varchar(255),
status int NOT NULL,
PRIMARY KEY (fileId)
);

解决方法:

这在SQLite FAQ. Question #1中得到解决.

哪个州:

How do I create an AUTOINCREMENT
field?

Short answer: A column declared
INTEGER PRIMARY KEY will
autoincrement.

Here is the long answer: If you
declare a column of a table to be
INTEGER PRIMARY KEY, then whenever you
insert a NULL into that column of the
table, the NULL is automatically
converted into an integer which is one
greater than the largest value of that
column over all other rows in the
table, or 1 if the table is empty. (If
the largest possible integer key,
9223372036854775807, then an unused
key value is chosen at random.) For
example, suppose you have a table like
this:

CREATE TABLE t1( a INTEGER PRIMARY
KEY, b INTEGER ); With this table,
the statement

INSERT INTO t1 VALUES(NULL,123); is
logically equivalent to saying:

INSERT INTO t1 VALUES((SELECT max(a)
FROM t1)+1,123);
There is a function
named sqlite3_last_insert_rowid()
which will return the integer key for
the most recent insert operation.

Note that the integer key is one
greater than the largest key that was
in the table just prior to the insert.
The new key will be unique over all
keys currently in the table, but it
might overlap with keys that have been
prevIoUsly deleted from the table. To
create keys that are unique over the
lifetime of the table, add the
AUTOINCREMENT keyword to the INTEGER
PRIMARY KEY declaration. Then the key
chosen will be one more than than the
largest key that has ever existed in
that table. If the largest possible
key has prevIoUsly existed in that
table, then the INSERT will fail with
an sqlITE_FULL error code.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐