我的数据库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;
}
答案 0 :(得分:0)
您需要在查询中传递Where子句。它是query()
的第4个参数。需要String
,您不应该包含Sqlite3关键字WHERE(Android会为您执行此操作)。该子句的结构可以像MySQLiteHelper.COLUMN_MESSAGE+"="+"r"
答案 1 :(得分: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);
答案 2 :(得分: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);
}