android - 根据条件查询获取行数

标签 android sqlite count cursor

在我的应用程序中,我需要获取“状态”列中具有特定值的总行数。这是我尝试过的代码,

String unreadquery="(SELECT COUNT(*)FROM TABLE_MESSAGES WHERE KEY_STATUS="0")";
Cursor cursor = db.rawQuery(unreadquery, null);

int a=cursor.getCount();

但查询语句中显示错误。如何获取特定列中值为 0 的行数?

谢谢!

这是创建表的代码:

public static final String TABLE_MESSAGES = "Messages";

String CREATE_MESSAGES_TABLE = "CREATE TABLE " + TABLE_MESSAGES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FROM + " TEXT," + KEY_TO + " TEXT,"
            + KEY_BODY + " TEXT," + KEY_DATE + " TEXT," + KEY_STATUS + " TEXT" + ")";
            db.execSQL(CREATE_MESSAGES_TABLE);

最佳答案

如果KEY_STATUS是文本列,请使用单引号。另外,根据您的评论,TABLE_MESSAGESKEY_STATUS 是定义表名称和列名称的常量。

String unreadquery="SELECT COUNT(*) FROM " 
    + TABLE_MESSAGES + " WHERE " + KEY_STATUS + "='0'";

关于android - 根据条件查询获取行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20565035/

相关文章:

java - 从字符串中删除 URL 前缀(http :/, www 等)

android - 如何使android对话框模态?

android - 下载的 SQLite 数据库和泄漏警告

Sqlite3 : Creating a new table from an old table is not populating the sqlite_sequence table correctly

c++ - 在没有库的情况下计算浮点值的小数位数?

sql - 当 Count(*) 结果为 Null 时返回 1 而不是 0

sql - 在新列中标记重复值

android - 屏幕解锁时的 Activity 句柄

android - Android通知中来自外部网址的setSmallIcon

sqlite - 为什么需要多次调用 zumero_sync?