![]() In this case the result was 2, because this was the second record I’ve inserted into this table. Now, just use the SQLite last_insert_rowid() function to get the value of the SQLite autoincrement field that was just generated: sqlite> select last_insert_rowid() Tables may be created in the main database, the temp database, or in any attached database. However, interestingly SQLite allows usage of AUTOINCREMENT keyword just after an obligatory PRIMARY KEY keyword. The database in which the new table is created. Next, insert a record into this table, passing a null value into the SQLite autoincrement field: sqlite> INSERT INTO salespeople VALUES (null, 'Fred', 'Flinstone', 10.0) A CREATE TABLE command specifies the following attributes of the new table: The name of the new table. (Note that this field is referred to as an autoincrement field, serial field, or identity column in other databases I have worked with.) In fact, you don't even need autoincrementTrue or db.Sequence('seqregid', start1, increment1), as SQLAlchemy will automatically set the first Integer PK column that's not marked as a FK as autoincrementTrue. ![]() Here’s a short, complete example of how this works.įirst, let’s assume we have a SQLite database table defined like this: CREATE TABLE salespeople (Īs you can see, the id field is the PRIMARY KEY field for this SQLite table. ![]() ![]() How to get the SQLite autoincrement (primary key) value after an insert You can get the integer value of the primary key field from the last insert into an autoincrement field using a SQLite function named last_insert_rowid(), as shown in the example below. SQLite autoincrement FAQ: How do I get the autoincrement value from my last SQLite INSERT command? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |