17

I am using sqlite with python 2.5. I get a sqlite error with the syntax below. I looked around and saw AUTOINCREMENT on this page http://www.sqlite.org/syntaxdiagrams.html#column-constraint but that did not work either. Without AUTO_INCREMENT my table can be created.

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)
);

5 Answers5

42

This is addressed in the SQLite FAQ. Question #1.

Which states:

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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
11

It looks like AUTO_INCREMENT should be AUTOINCREMENT see http://www.sqlite.org/syntaxdiagrams.html#column-constraint

Andrew Cox
  • 10,672
  • 3
  • 33
  • 38
6

You could try

CREATE TABLE fileInfo
(
fileid INTEGER PRIMARY KEY AUTOINCREMENT,
name STRING,
status INTEGER NOT NULL
);
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
1

We just changed the order from

NOT NULL, AUTO_INCREMENT 

to

AUTO_INCREMENT NOT NULL,
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
henrik
  • 11
  • 1
0

an example :

cursor.execute("CREATE TABLE users(\
    user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
    user_name VARCHAR(100) NOT NULL)")
nighthaven
  • 21
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 29 '22 at 04:39