将SQLite查询转换为Realm查询Android

时间:2017-02-15 15:21:45

标签: android sqlite realm realm-mobile-platform

使用此方法返回按查询过滤的对象列表

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查询中转换它?

最重要的是,我有关于WHERE CLAUSE的问题。

我需要这个WHERE CLAUSE,因为我需要通过FirstName + LastName或LastName + FirstName进行搜索。

例如 John Smith Smith John总是会留下记录。

1 个答案:

答案 0 :(得分:0)

    realm.where(People.class)
            .beginGroup()
            .equalTo("FirstName", "John").equalTo("LastName", "Smith")
            .endGroup()
            .or()
            .beginGroup()
            .equalTo("LastName", "John").equalTo("FirstName", "Smith")
            .endGroup()
            .findAll();