使用 Java 中的 sqlite 获取最后插入的 id 的最佳方法是什么? [英] What's the best way to get the last inserted id using sqlite from Java?

查看:17
本文介绍了使用 Java 中的 sqlite 获取最后插入的 id 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Java 中的 sqlite 获取最后插入的 id 的最佳方法是什么?谷歌给了我不同的答案——有人说选择最后一个插入行;其他人说调用 statement.getGeneratedKeys().最好的路线是什么?(我只想返回 id,而不是将它用于其他插入或任何内容.)

What's the best way to get the last inserted id using sqlite from Java? Google is giving me different answers--some say select the last-insert-rowid; others say call statement.getGeneratedKeys(). What's the best route to take? (I just want to return the id, not use it for other inserts or anything.)

推荐答案

如果您的 JDBC 驱动程序支持 getGeneratedKeys(),请使用它.您不想在插入后尝试自己获取密钥.如果您的驱动程序不支持 getGeneratedKeys() 那么我会在插入之前从键中获取下一个值.

Use getGeneratedKeys() if your JDBC driver supports it. 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.

这篇关于使用 Java 中的 sqlite 获取最后插入的 id 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆