简体   繁体   English

使用Java中的sqlite获取最后插入的ID的最佳方法是什么?

[英]What's the best way to get the last inserted id using sqlite from Java?

What's the best way to get the last inserted id using sqlite from Java? 使用Java中的sqlite获取最后插入的ID的最佳方法是什么? Google is giving me different answers--some say select the last-insert-rowid; Google给我不同的答案-有人说选择last-insert-rowid; others say call statement.getGeneratedKeys(). 其他人说调用statement.getGeneratedKeys()。 What's the best route to take? 最好的路线是什么? (I just want to return the id, not use it for other inserts or anything.) (我只想返回id,而不将其用于其他插入或其他操作。)

Either approach executes the same exact SQL statement, but java.sql.Statement.getGeneratedKeys() is more portable to different underlying databases. 两种方法都执行相同的确切SQL语句,但是java.sql.Statement.getGeneratedKeys()更易于移植到不同的基础数据库。

Using either the sqlite3_last_insert_rowid() C API function or the last_insert_rowid() SQL function is the correct way to retrieve the rowid generated during the last insert. 使用sqlite3_last_insert_rowid() C API函数或last_insert_rowid() SQL函数是检索在上一次插入期间生成的行ID的正确方法。

SQLite supports the SQL scalar function syntax as: SQLite支持以下SQL标量函数语法:

SELECT function-name();

Therefore, if you do not have access to the C API directly, you can invoke the scalar function last_insert_rowid() via a SELECT statement. 因此,如果您无权直接访问C API,则可以通过SELECT语句调用标量函数last_insert_rowid()

If you look at the source code for the SQLiteJDBC implementation (is this what you're using?) you will see that this is exactly what the author of that JDBC wrapper has done: 如果查看SQLiteJDBC实现的源代码 (这是您正在使用的吗?),您将看到这正是该JDBC包装程序的作者所做的:

ResultSet getGeneratedKeys() throws SQLException {
    if (getGeneratedKeys == null) getGeneratedKeys = conn.prepareStatement(
        "select last_insert_rowid();");
    return getGeneratedKeys.executeQuery();
}

Use getGeneratedKeys() if your JDBC driver supports it. 如果您的JDBC驱动程序支持getGeneratedKeys() ,请使用它。 You don't want to muck around trying to get the key yourself after the insert. 您不想在插入后尝试自己获取密钥。 If your driver does not support getGeneratedKeys() then I would get the next value from the key before the insert. 如果您的驱动程序不支持getGeneratedKeys()那么我将在插入之前从键中获取下一个值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Basepeer-获取最后插入记录的ID - Java Basepeer - Get ID of The Last Inserted Record 如何使用Java从mongodb获取最后插入的N条记录? - How to get the last inserted N records from mongodb using Java? 使用Java,用数组中的值更新每一行中的一列的最佳方法是什么? (SQLite) - Using Java, what is the best way to update a column in every row with values from an array? (SQLite) java,mysql-为什么不工作,从一个表中获取最后一个插入的ID,然后将其再次插入另一个表中? - java,mysql - why not working get last inserted id from one table and insert it again on another table? mysql,java - 从一个表中获取最后插入的自动增量 id 并将其再次插入到另一个表中 - mysql, java - Get last inserted auto increment id from one table and insert it again on another table Java Hibernate中最后插入的ID - Last inserted Id in Java Hibernate 在带有 Java 驱动程序的 mongoDB 中获取最后插入文档的 ID - Get ID of last inserted document in a mongoDB w/ Java driver 使用 java.time API 获取一个月天数的最佳方法是什么? - What's the best way to get the number of days in a month using the java.time API? JAVA:从 HTTPS URL 获取响应代码的最快和最好的方法是什么? - JAVA: What's the fastest and best way to get a response code from HTTPS URL? 在 Java 中获取控制台输入的最佳方式是什么? - What's the best way to get console-input in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM