Delphi XE4. dbExpress for Sqlite3.
Hello everyone. I'm trying to obtain autoincremented primary key value of inserted row. Like this:
SQLQuery.CommandText := 'INSERT INTO tbl1 (id, name) VALUES (:id, :name)';
SQLQuery.ParamByName('id').ParamType := ptInputOutput;
SQLQuery.ExecSql();
Inserting works fine, but when I'm trying to get back my id parameter, it is always 0:
SQLQuery.ParamByName('id').Value <--- 0
How can I get autogenerated key for inserted row atomically?
Thank you!
Output parameters in INSERT query
Re: Output parameters in INSERT query
Hello,
To retrieve the value of the auto-incremental field, you should use the SQLite last_insert_rowid function
To retrieve the value of the auto-incremental field, you should use the SQLite last_insert_rowid function
Code: Select all
SELECT last_insert_rowid()
Re: Output parameters in INSERT query
Thank you. But is this will be an atomic operation? I mean what if someone else inserted something in this table?AlexP wrote:Hello,
To retrieve the value of the auto-incremental field, you should use the SQLite last_insert_rowid function
Code: Select all
SELECT last_insert_rowid()
Re: Output parameters in INSERT query
Found answer on last question myself here http://www.sqlite.org/c3ref/last_insert_rowid.html
Thank you.
Thank you.
Re: Output parameters in INSERT query
Hello,
Glad to see that you solved the problem. If you have any other questions, feel free to contact us
Glad to see that you solved the problem. If you have any other questions, feel free to contact us