getGeneratedKeys
(1) By anonymous on 2023-11-19 15:25:49 [link] [source]
The method getGeneratedKeys is deprecated by jdbc driver since version 3.43 (https://www.javadoc.io/doc/org.xerial/sqlite-jdbc/3.43.2.0/deprecated-list.html).
This method was convenient to retrieve values of auto-incremented values when executing requests of type insert into sometable default values
.
I think a lot of jdbc driver clients used it in order to get generated keys. Baeldung uses it as example in it's tutorials (https://www.baeldung.com/jdbc-returning-generated-keys). Moreover, many other sql databases support it.
Please can you restore it or describe what is the good practice to get the generated keys ?
(2) By Stephan Beal (stephan) on 2023-11-19 15:37:25 in reply to 1 [link] [source]
Please can you restore it or describe what is the good practice to get the generated keys ?
You'll need to contact the maintainer of the JDBC driver, which is a third-party project.
The C API (the part of sqlite which is maintained here) does not have any such API.
According to the link you posted, the docs for the newly-deprecated function say:
Not exactly sure what this function does, as it is not implementing any interface, and is not used anywhere in the code.
Given that, it's difficult to understand how any clients can possibly rely on it.
(3) By Rowan Worth (sqweek) on 2023-11-20 04:51:57 in reply to 2 [link] [source]
The C API (the part of sqlite which is maintained here) does not have any such API.
Not entirely true -- sqlite3_last_insert_rowid is fit for purpose when used against INTEGER PRIMARY KEY tables.
Not exactly sure what this function does, as it is not implementing any interface, and is not used anywhere in the code.
This is an interesting bit of information, as there is definitely a JDBC API called java.sql.Statement.getGeneratedKeys()
.
Looking at the sqlite-jdbc change log, the methods which have been dropped are:
org.sqlite.core.CoreDatabaseMetaData.getGeneratedKeys()
org.sqlite.jdbc3.JDBC3DatabaseMetaData.getGeneratedKeys()
And indeed the JDBC API for java.sql.DatabaseMetaData
does not [currently] define a getGeneratedKeys()
method. sqlite-jdbc version 3.43.2.0 still supports java.sql.Statement.getGeneratedKeys():
Which answers the OP's question, ie. ignore the change log and continue using getGeneratedKeys()
on your java.sql.Statement
objects as normal.
(4) By anonymous on 2023-11-20 08:38:27 in reply to 3 [link] [source]
This topic is already discussed in many issues on sqlite-jdbc github (for instance https://github.com/xerial/sqlite-jdbc/issues/980)
Actually the driver's getGeneratedKeys()
method was an alias of select last_insert_rowid();
request, which could be a problem on multithread systems.
Now the good practice to get auto-generated keys seems to use the RETURNING
clause. Then "insert into someTable default values;
" statement should be changed to
insert into someTables default values returning *;
(5) By Code Hz (codehz) on 2023-11-20 09:20:22 in reply to 1 [source]
I think you can get the generated id via something like
insert into a (text) values ('123') returning id;