0

我的数据库 id 、消息和消息状态中有三列,我只想从列表中选择消息状态为“r”的行,并且只想从查询中返回光标以获取 id 和消息。我是数据库新手,请帮忙。我当前选择所有行的代码是:

private String[] allColumns = { MySQLiteHelper.COLUMN_ID,MySQLiteHelper.COLUMN_MESSAGE };

public List<Message> getAllMessages() {
    List<Message> message = new ArrayList<Message>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message1 = cursorToMessage(cursor);
      message.add(message1);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return message;
  }
4

3 回答 3

0

试试这个代码,

public static final String KEY_ROWID = "row";
public static final String KEY_NAME = "name";

public Cursor fetchNamesByConstraint(String filter) {
    Cursor cursor = mDb.query(true, DATABASE_NAMES_TABLE, null, "row LIKE '%" + filter + "%' or name LIKE '%" + filter + "%'",null, null, null, null);
}
于 2014-03-23T05:11:56.320 回答
0

您需要在查询中传递 Where 子句。它是第 4 个参数query()。它需要一个String, 并且您不应该包含 Sqlite3 关键字 WHERE (Android 会为您执行此操作)。该子句的结构可以像MySQLiteHelper.COLUMN_MESSAGE+"="+"r"

于 2013-09-08T13:20:51.237 回答
0
 SQLiteDatabase database = this.getReadableDatabase();
 String queryz = "SELECT " + COLUMN_ID + "," + COLUMN_MESSAGE + " FROM " + TABLE_NAME + " WHERE " + COLUMN_MESSAGE_STATUS + "= 'r'";
 Cursor c = database.rawQuery(queryz, null);
于 2013-09-08T13:27:51.453 回答