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

标签 android sqlite 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
        }

其中 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/

相关文章:

android - TableLayout 中的按钮在 Android 1.6 和 2.1 上裁剪(但在 1.5 或 2.2 上不裁剪)

c - 关于 SQLITE 数据库的 C API

SQL 不带游标的存储过程输出多行

android - onListitemClick 从数据库获取列值

java - Android - 具有 2 个源文件夹的项目的 gradle 构建失败

php - 使用 MySQL 的 Android 登录页面

android - 在 android 中使用 PrePopulated SQlite 数据库

javascript - 向表格行添加光标样式以指示它们可单击是否合理

android - 在 manifest.json 中添加了图标,但在启动画面中没有图标

android - 无法将表格迁移到Room do,将 bool 值保存在Sqlite中的方式出错