Sqlite JDBC driver does not support RETURN_GENERATED_KEYS

I use Xerial latest jdbc driver for sqlite (version 3.7.2). It does not seem to have support for the RETURN_GENERATED_KEYS operator. I continue to "do not implement SQLite JDBC driver" in SQLException.

I really do not want to make another choice call (for example, select id from the order of tasks by 1 limit 1) to get the last identifier created by the auto-increment field. I need to use this id to populate a field in another table.

Are there any better ways to achieve this with the sqlite-jdbc driver?

+3
source share
2 answers

/ JDBC (SQLiteJDBC, , ), a SELECT last_insert_rowid() . , .

connection = database.getConnection();
connection.setAutoCommit(false); // Starts transaction.
preparedStatement = connection.prepareStatement(INSERT_SQL);
preparedStatement.setSomething(something);
// ...
preparedStatement.executeUpdate();
statement = connection.createStatement();
generatedKeys = statement.executeQuery("SELECT last_insert_rowid()");
if (generatedKeys.next()) {
    generatedKey = generatedKeys.getLong(1);
}
connection.commit(); // Commits transaction.
+8

sqlite-jdbc-3.7.2.jar , RETURN_GENERATED_KEYS , RETURN_GENERATED_KEYS statement.execute(sql) resultset = statement.getGeneratedKeys() meta = resultset.getMetaData() last_insert_rowid() . , resultset.getInt("last_insert_rowid()") .

+4

Source: https://habr.com/ru/post/1777325/


All Articles