No autoincrement for Integer Primary key in sqlite3

前端 未结 2 954
無奈伤痛
無奈伤痛 2021-02-07 03:24

In the sqlite3 faq, it is mentioned that an integer primary key being fed a null value would autoincrement. But this is not happening for me.

to replicate, a table in sq

2条回答
  •  执念已碎
    2021-02-07 04:09

    The insert syntax provided above does not seem to work in the absence of not null.

    Here's an example - note that the ID field is not autoincremented even though I use the insert format that you specified above.

    sqlite> .schema logTable
    CREATE TABLE logTable (ID INTEGER PRIMARY_KEY, ts REAL, level TEXT, message TEXT);
    sqlite> INSERT into LOGTABLE (ts, level, message) VALUES (111, "autoinc test", "autoinc test");
    sqlite> select * from logtable where ts = 111;
    |111.0|autoinc test|autoinc test
    sqlite> 
    

    It does work with the NOT NULL workaround.

    sqlite> create TABLE logTable (ID INTEGER PRIMARY KEY NOT NULL, ts REAL, level TEXT, message TEXT);
    sqlite> INSERT into LOGTABLE (ts, level, message) VALUES (222, "autoinc test", "autoinc test"); 
    sqlite> select * from logtable where ts = 222;
    1|222.0|autoinc test|autoinc test
    

    I apologize for posting this as a new answer instead of commenting on the previous answer, but my reputation score is too low to add comments, and I thought that it was important to note that the alternate insert statement is not an adequate workaround.

提交回复
热议问题