使用此方法返回按查询过滤的对象列表
public List<table_People> getPeople(String filter)
{
List<table_People> items = new ArrayList<>();
filter = filter.trim();
filter = filter.toUpperCase();
String selectQuery = "SELECT * FROM People " +
"WHERE (UPPER(LastName || FirstName) LIKE '%" + filter + "%') " +
"OR (UPPER(FirstName || LastName) LIKE '%" + filter + "%') ";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
table_People item = new table_People(
cursor.getInt((cursor.getColumnIndex("ID"))),
cursor.getString((cursor.getColumnIndex("FirstName"))),
cursor.getString((cursor.getColumnIndex("LastName")))
);
items.add(item);
} while (cursor.moveToNext());
}
return items;
}
如何在Realm query中进行转换?
最重要的是,我对WHERE子句有一些问题。
我需要这个WHERE子句,因为我需要按FirstName + LastName或LastName + FirstName进行搜索。
例如,John Smith
Smith John
总是返回一条记录。
发布于 2017-02-16 02:14:37
realm.where(People.class)
.beginGroup()
.equalTo("FirstName", "John").equalTo("LastName", "Smith")
.endGroup()
.or()
.beginGroup()
.equalTo("LastName", "John").equalTo("FirstName", "Smith")
.endGroup()
.findAll();
https://stackoverflow.com/questions/42253368
复制相似问题