从sqlite数据库Android获取最后插入的值 [英] Get last inserted value from sqlite database Android

查看:33
本文介绍了从sqlite数据库Android获取最后插入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Android 的 sqlite 数据库中获取最后插入的 rowid.我已经阅读了很多关于它的帖子,但无法让一个工作.这是我的方法:

I am trying to get the last inserted rowid from a sqlite database in Android. I have read a lot of posts about it, but can't get one to work. This is my method:

 public Cursor getLastId() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_WID}, KEY_WID + "=" + MAX(_id), null, null, null, null, null);}

我已经尝试过 MAX,但我一定是用错了.还有别的办法吗?

I have tried with MAX, but I must be using it wrong. Is there another way?

推荐答案

实际上 SQLiteDatabase 类有它自己的 insert 方法,它返回新创建的行的 id.我认为这是获得新 ID 的最佳方式.您可以查看其文档 此处.

Well actually the SQLiteDatabase class has its own insert method which returns the id of the newly created row. I think this is the best way to get the new ID. You can check its documentation here.

我希望这会有所帮助.

这篇关于从sqlite数据库Android获取最后插入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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