当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to check a value in data base if exist or not

问题描述:

i'm trying to check if the database has a specific value from a specific column

this is my Function

 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;

}

but the result is always true ! even the value which i input is not exist

网友答案:

Your cursor is not null, thats why you're always in the {return true} path - db.rawQuery returns a new cursor instance.

Android provides a method Cursor.getCount() see here in the documentation 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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: