本文介绍了“选择 last_insert_rowid()"总是返回“0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对last_insert_rowid()"有疑问.
I have a problem with "last_insert_rowid()".
在我的 DB-Helper-Class 中,我正在执行以下操作:
In my DB-Helper-Class I am doing the following:
public int getLastID() {
final String MY_QUERY = "SELECT last_insert_rowid() FROM "+ DATABASE_TABLE5;
Cursor cur = mDb.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int ID = cur.getInt(0);
cur.close();
return ID;
}
但是当我按照我的意图调用它时:
But when I call this in my intent:
int ID = mDbHelper.getLastID();
Toast.makeText(this, "LastID: " + ID, Toast.LENGTH_SHORT).show();
我总是返回0".不可能,因为我的数据库中有很多条目,并且自动增量值应该高得多.
I get always "0" back. That can not be, cause I have a lot of entries in my DB and the autoincrement value should be much higher.
我做错了什么?
推荐答案
好的,这是正确的提示 John ;-)
OK, that was the right hint John ;-)
查询应如下所示:
public int getHighestID() {
final String MY_QUERY = "SELECT MAX(_id) FROM " + DATABASE_TABLE5;
Cursor cur = mDb.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int ID = cur.getInt(0);
cur.close();
return ID;
}
这篇关于“选择 last_insert_rowid()"总是返回“0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!