Csaba wrote:
> Is there any way to detect, based strictly on querying the structure of a
> table/database
> whether there is an AUTOINCREMENT set? That is to say, without analyzing
> the
> original SQL creation statement, and without inserting a new element into
> the database.
>
Yes there is. The autoincrement keyword can only be applied to an
integer primary key column, and when it is, sqlite creates a entry in
its internal sqlite_sequence table with the name set to the name of the
table.
Note, a table's sequence entry isn't created until the first row has
been added to the table. Also, the internal sequence table is not listed
by a normal .tables meta command, but it is listed if you use a wildcard
pattern.
BTW, the autoincrement keyword must appear after the integer primary key
phrase, not in the middle as you have show it.
Since autoincrement can only be used with integer primary key columns,
the column name in the table can be determined by looking at the output
of the pragma table_info() command. It will be the primary key column
indicated by the value 1 in the pk column.
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> create table t (id integer primary key autoincrement);
sqlite> .tables '%'
sqlite_sequence t
sqlite> .header on
sqlite> .mode column
sqlite> select * from sqlite_sequence;
sqlite> insert into t values(1);
sqlite> select * from sqlite_sequence;
name seq
---------- ----------
t 1
sqlite> pragma table_info(t);
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 id integer 0 1
HTH
Dennis Cote
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users