如何检查列中是否存在特定值?

Sab*_*bre 1 sqlite android cursor

我正在尝试检查列中是否存在该值.例如,我想检查该列是否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
        }
Run Code Online (Sandbox Code Playgroud)

entryId一些数字在哪里.

但我总是得到这样的东西:

W/ID COUNT(11303): android.database.sqlite.SQLiteCursor@41aa4c80.

我也尝试使用:

getStar = db.rawQuery("SELECT count(*) FROM "
+ TABLE_NAME + " WHERE artId = '" + entryId + "'", null);
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的结果.

所以我希望得到你的帮助.

Eri*_*ric 5

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
}
Run Code Online (Sandbox Code Playgroud)

我对上述结果有点不确定; 相反,你可以查询结果的数量:

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
}
Run Code Online (Sandbox Code Playgroud)