我正在尝试检查该值是否存在于列中。例如,我想检查 artId
列是否包含一些数值。我正在使用以下代码:
getStar = db.query(TABLE_NAME, new String[] { "artId" },
"artId = " + entryId, null, null,
null, null);
getStar.moveToFirst();
String star = getStar.toString();
Log.w("ID COUNT", star);
if(getStar != null) {
// do something
}
其中 entryId
是一些数字。
但我总是得到这样的结果:
W/ID 计数(11303):android.database.sqlite.SQLiteCursor@41aa4c80
。
我也尝试使用:
getStar = db.rawQuery("SELECT count(*) FROM "
+ TABLE_NAME + " WHERE artId = '" + entryId + "'", null);
但我得到了相同的结果。
所以我希望得到你的帮助。
最佳答案
Cursor
是有效的(非null
),是的,因为它没有错误。相反,检查它是否有任何数据。
getStar = db.rawQuery("SELECT count(*) FROM " + TABLE_NAME + " WHERE artId = '" + entryId + "'", null);
getStar.moveToFirst();
if (getStar.getInt(0)) { // This will get the integer value of the COUNT(*)
// It has data
}
我对上面的结果有点不确定;相反,您可以查询结果数:
getStar = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE artId = '" + entryId + "'", null);
if (getStar.getCount() > 0) { // This will get the number of rows
// It has data
}
关于android - 如何检查列中是否存在特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679584/