ActiveAndroid - 如何获取最后一行ID?

时间:2015-01-12 18:21:20

标签: android sqlite activeandroid

我需要在ActiveAndroid数据库表中获取最后插入的行ID,但我无法在文档中找到有关该行的任何信息。有没有正确的方法来做到这一点?

3 个答案:

答案 0 :(得分:1)

获取表格的最后一条记录,然后选择像这样的自动生成的ID ...

Model model = new Select().from(Model.class).orderBy("id DESC").executeSingle();
long id = (model != null) ? model.getId() : 0;

我已检查 Null ,因为如果表为空,则可能为空。

或点击以下内容......

long id = new Select().from(Model.class).orderBy("id DESC").executeSingle().getId();

答案 1 :(得分:0)

如果是sqlite数据库并且您可以查询此表,则可以在查询中使用sql函数:

last_insert_rowid() last_insert_rowid()函数返回调用该函数的数据库连接中最后一行插入的ROWID。 ......

文件取自: http://www.sqlite.org/lang_corefunc.html

答案 2 :(得分:0)

int insert_id;

User user = new User();
user.name = "Louis";
user.password = 12345;
user.save();

insert_id = user.getId().intValue();

希望这可以帮到你!

相关问题