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

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

问题描述

使用Java中的sqlite获取最后插入的id的最佳方法是什么?谷歌给了我不同的答案 - 有人说选择last-insert-rowid;其他人说call 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.)

推荐答案

如果你使用getGeneratedKeys() JDBC驱动程序支持它。你不想在插入后尝试自己获取密钥。如果你的驱动程序不支持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天全站免登陆