[英]SQLitedatabase query
I have three columns in my database id ,message and message status and I only want to select only those rows from the list whose message status is 'r' and want to return the cursor from query for only id and message. 我的数据库ID,消息和消息状态中有三列,我只想从列表中仅选择消息状态为“ r”的那些行,并且只想从查询中返回游标以获取ID和消息。 I am new to databases,Please help.
我是数据库新手,请帮忙。 My current code which is selecting all the rows is:
我当前选择所有行的代码是:
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;
}
You need to pass a Where clause yo your query. 您需要在查询中传递Where子句。 It is the 4th parameter of the
query()
. 它是
query()
的第四个参数。 It takes a String
, and you should not include the Sqlite3 keyword WHERE (Android does that for you). 它需要一个
String
,并且您不应包括Sqlite3关键字WHERE(Android会为您提供)。 The clause can be structured like MySQLiteHelper.COLUMN_MESSAGE+"="+"r"
该子句的结构类似于
MySQLiteHelper.COLUMN_MESSAGE+"="+"r"
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);
Try this code, 试试这个代码,
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.