【问题标题】:SqlDelight not generating SQL Query String for SQL StatementSqlDelight 没有为 SQL 语句生成 SQL 查询字符串
【发布时间】:2017-04-27 13:02:25
【问题描述】:

SqlDelight documentation的示例中,SqlDelight从HockeyPlayer.sq文件生成的HockeyPlayerModel在抽象类public abstract class HockeyPlayer implements HockeyPlayerModel中实现

在这个类中,字符串SELECT_ALL_INFO 作为查询传递给db.rawQuery(SELECT_ALL_INFO, new String[0])。字符串SELECT_ALL_INFO 是由HockeyPlayer.sq 中的select_all_info 语句生成的。但是,在我的情况下,我的语句没有生成字符串。这是为什么呢?

我的声明

names_for_groups:
SELECT DISTINCT name, exercise_set_group FROM exercise_set JOIN exercises
USING (exercise_id) WHERE workout_FK = ? ORDER BY exercise_set_group ASC ;

我对 SqlDelight 生成的ExerciseSetModel 的实现

@AutoValue
public abstract class DbExerciseSet implements ExerciseSetModel, DbItem {
    public static final Factory<DbExerciseSet> FACTORY = new Factory<>(AutoValue_DbExerciseSet::new);
    public static final RowMapper<DbExerciseSet> MAPPER = FACTORY.select_allMapper();

    public static final RowMapper<NamesForGroups> NAMES_FOR_GROUPS_MAPPER =
            FACTORY.names_for_groupsMapper(AutoValue_DbExerciseSet_NamesForGroups::new);

    public List<NamesForGroups> namesForGroups(SQLiteDatabase db) {
        List<NamesForGroups> namesForGroupsList= new ArrayList<>();
        Cursor cursor = db.rawQuery(NAMES_FOR_GROUPS, new String[0]);
            while (cursor.moveToNext() && NAMES_FOR_GROUPS_MAPPER.map(cursor) != null) {
                //NamesForGroups namesForGroups = NAMES_FOR_GROUPS_MAPPER.map(cursor);
                namesForGroupsList.add(NAMES_FOR_GROUPS_MAPPER.map(cursor));
            }

        return namesForGroupsList;
    }

    @AutoValue
    public abstract static class NamesForGroups implements Names_for_groupsModel {}

    @AutoValue
    public abstract static class Exercises implements ExerciseModel {}
}

需要明确的是,变量引用 NAMES_FOR_GROUPS 在db.rawQuery(NAMES_FOR_GROUPS, new String[0]) 行中找不到

【问题讨论】:

    标签: java android sqlite stored-procedures sqldelight


    【解决方案1】:

    文档需要更新,我会更新的。我们不再在 SQLDelight 0.6.+ 中生成字符串,而是工厂上有一个方法返回用于查询的SQLDelightStatement

    public List<NamesForGroups> namesForGroups(SQLiteDatabase db) {
        List<NamesForGroups> namesForGroupsList= new ArrayList<>();
        SQLDelightStatement query = FACTORY.name_for_groups();
        Cursor cursor = db.rawQuery(query.statement, query.args);
            while (cursor.moveToNext() && NAMES_FOR_GROUPS_MAPPER.map(cursor) != null) {
                //NamesForGroups namesForGroups = NAMES_FOR_GROUPS_MAPPER.map(cursor);
                namesForGroupsList.add(NAMES_FOR_GROUPS_MAPPER.map(cursor));
            }
    
        return namesForGroupsList;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多