使用ORMLite按列搜索编写查询

时间:2017-09-17 18:20:17

标签: android ormlite

我必须列User和MyDream。我需要使用orbaseite创建查询,使用firebase_id显示my_dream。 这是我的SQL查询

从MyDream中选择my_dream_id,title_dream,descryption_dream,saveDataDream,User.user_id = MyDream.user_user_id和User.firebaseId =' 56789'

我不想在Java中编写查询

column database

1 个答案:

答案 0 :(得分:0)

首先,您需要定义您的类:

public class User {
    @DatabaseField(id = true)
    private Integer user_id;
    @DatabaseField
    private String email;
    @DatabaseField
    private Integer firebase_id;
    @DatabaseField
    private String fullname;
    @DatabaseField
    private String photo_url;
    @ForeignCollectionField
    private LazyForeignCollection<Dream, Integer> dreams;
}

public class Dream {
    @DatabaseField(id = true)
    private Integer my_dream_id;
    @DatabaseField
    private String description_dream;
    @DatabaseField
    private ?? saveDataDream;
    @DatabaseField
    private String titleDream;
    @DatabaseField(columnName = "user_user_id", foreign = true)
    private User user;
}

然后,正如我问过here,您需要使用Join来查询结果

它的解释相同,但这里是你的例子的代码

Dao<User, Integer> userDao = DaoManager.createDao(getConnectionSource(), User.class);
Dao<Dream, Integer> dreamDao = DaoManager.createDao(getConnectionSource(), Dream.class);

QueryBuilder<User, Integer> userQa= userDao.queryBuilder();
userQa.where().eq("firebaseId", 56789);
QueryBuilder<Dream, Integer> dreamQa = dreamDao.queryBuilder();

users = userQa.join(dreamQa).query();

然后您可以使用users.get(i).getDreams()从用户的对象iterator()访问梦想,如下所示:

CloseableIterator<Dream> itDream = users.get(id).getDreams().closeableIterator();
try {
    while (itDream.hasNext()) {
        Dream dream = itDream.next();
    }
} finally {
    try {
        itDream.close();
    } catch (IOException e) {
        Log.e(TAG, "FAIL", e);
    }
}