Android 和 SQLite - 从表中检索最大 id [英] Android and SQLite - retrieve max id from table
本文介绍了Android 和 SQLite - 从表中检索最大 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建方法来从我的表中检索最大 ID,但我遇到了问题.
I am trying to create method to retrieve the max id from my table but I've problem.
这是我的方法.它正在工作,但返回值为0,
This is my method. It's working, but return value is 0,
public int getLastId() {
openDB();
int id = 0;
final String MY_QUERY = "SELECT MAX(_id) AS _id FROM organize";
Cursor mCursor = mDb.rawQuery(MY_QUERY, null);
try {
if (mCursor.getCount() > 0) {
mCursor.moveToFirst();
id = mCursor.getInt(mCursor.getColumnIndex(MY_QUERY));
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
closeDB();
}
return id;
}
我能解决这个问题吗,非常感谢
can I fix this problem, thanks a lot
推荐答案
重写这一行
id = mCursor.getInt(mCursor.getColumnIndex(MY_QUERY));
到
id = mCursor.getInt(mCursor.getColumnIndex("_id"));
或者更好
id = mCursor.getInt(0);//there's only 1 column in cursor since you only get MAX, not dataset
看看 LogCat,它会告诉你你的问题.
And look at LogCat, it will tell you about your problem.
这篇关于Android 和 SQLite - 从表中检索最大 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文