我正在尝试检查数据库是否具有特定列的特定值 这是我的功能
public boolean Check(String num) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_RECORD + " WHERE "
+ KEY_NUM + "= '" + num + "'",null);
if (c == null) {
// doesn't exists therefore insert record.
return false;
} else
return true;
}
但结果总是如此!甚至我输入的值也不存在
答案 0 :(得分:1)
您的游标不为空,这就是为什么您总是在{return true}路径中 - db.rawQuery
返回一个新的游标实例。
Android提供了一种方法Cursor.getCount()
,请参阅文档docs。
public boolean Check(String num) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_RECORD + " WHERE "
+ KEY_NUM + "= '" + num + "'",null);
// Let's ask the cursor how many rows are fetched
if (c.getCount() == 0) {
// doesn't exists therefore insert record.
// we have no result
return false;
} else
// there is min. 1 row
return true;
}