如何在ORMLite中构建Select查询(更多没有连接的表)

时间:2014-03-22 13:35:37

标签: android sql select join ormlite

在ORMLite中查询这个的最佳方法是什么?

SELECT JOB.TASK_JOB,
            JOB.NAME_JOB,
            JOB_SETTINGS.DAY1,
            JOB_SETTINGS.DAY2,
            JOB_SETTINGS.DAY3,
            JOB_SETTINGS.DAY4,
            JOB_SETTINGS.DAY5,
            JOB_SETTINGS.DAY6,
            JOB_SETTINGS.DAY7,
            JOB_SETTINGS.HOURSPERWEEK,
            JOB_SETTINGS.PAYPERHOUR,
            JOB_SETTINGS.VARIABLEHOURS,
            WORKINGDAY.DATE_WD,
            WORKINGDAY.H_BREAK,
            WORKINGDAY.H_REAL,
            WORKINGDAY.H_IN,
            WORKINGDAY.H_OUT
        FROM JOB, JOB_SETTINGS,WORKINGDAY
        WHERE JOB.ID_JOB = JOB_SETTINGS.ID_JOB
        AND JOB_SETTINGS.ID_JOB = WORKINGDAY.ID_JOB
        AND JOB.ID_JOB = 2

我为每个表都有一个类(标准ORMLite),但我不了解如何构建此查询...

感谢!!!

修改

我写了这段代码,但实际上我无法继续,因为我对OrmLite语法感到困惑......

DbHelperJob queryDao = new DbHelperJob(getActivity());

    QueryBuilder<Job, String> qJob = queryDao.getJobDao().queryBuilder();
    qJob.selectColumns("NAME_JOB", "TASK_JOB");

    QueryBuilder<JobSettings, String> qJobSett = queryDao.getJobSettDao().queryBuilder();
    qJobSett.selectColumns("DAY1", "DAY2", "DAY3", "DAY4", "DAY5", "DAY6", "DAY7", "HOURSPERWEEK", "PAYPERHOUR", "VARIABLEHOURS");

    QueryBuilder<WorkingDay, String> qWD = queryDao.getWDDao().queryBuilder();
    qWD.selectColumns("DATE_WD", "H_BREAK", "H_REAL", "H_IN", "H_OUT");

3 个答案:

答案 0 :(得分:0)

Android查询有参数:

  1. - 我们想要获取的字段列表
  2. 选择 - 字符串WHERE条件
  3. selectionArgs -
  4. 的参数数组
  5. 选择即可。在选择中,您可以使用&#34;?&#34;并将被这些价值取代。
  6. groupBy - 分组
  7. - 使用汇总功能的条件
  8. orderBy - 排序
  9. 使用它。

答案 1 :(得分:0)

这与ORMLite无关,但是在SQLiteQueryBuilder中有一个非常方便的查询工具。你可以这样做:

private static final String COLS = new String[] {
    JOB.TASK_JOB,
    JOB.NAME_JOB,
    JOB_SETTINGS.DAY1,
    JOB_SETTINGS.DAY2,
    JOB_SETTINGS.DAY3,
    JOB_SETTINGS.DAY4,
    JOB_SETTINGS.DAY5,
    JOB_SETTINGS.DAY6,
    JOB_SETTINGS.DAY7,
    JOB_SETTINGS.HOURSPERWEEK,
    JOB_SETTINGS.PAYPERHOUR,
    JOB_SETTINGS.VARIABLEHOURS,
    WORKINGDAY.DATE_WD,
    WORKINGDAY.H_BREAK,
    WORKINGDAY.H_REAL,
    WORKINGDAY.H_IN,
    WORKINGDAY.H_OUT
}

private static String SEL =
    "JOB.ID_JOB = JOB_SETTINGS.ID_JOB"
        + "AND JOB_SETTINGS.ID_JOB = WORKINGDAY.ID_JOB"
        + "AND JOB.ID_JOB = 2"

private static String TABLES = "JOB, JOB_SETTINGS, WORKINGDAY""


// in the query method...
    db.execSQL(
        SQLiteQueryBuilder.buildQueryString(
            false, TABLES, COLS, SEL, null, null, null, null);

答案 2 :(得分:0)

你可以使用你的Dao的queryRaw()方法。例如

GenericRawResults<String[]> rawResults = queryDao.queryRaw("SELECT JOB.TASK_JOB,
                JOB.NAME_JOB,
                JOB_SETTINGS.DAY1,
                JOB_SETTINGS.DAY2,
                JOB_SETTINGS.DAY3,
                JOB_SETTINGS.DAY4,
                JOB_SETTINGS.DAY5,
                JOB_SETTINGS.DAY6,
                JOB_SETTINGS.DAY7,
                JOB_SETTINGS.HOURSPERWEEK,
                JOB_SETTINGS.PAYPERHOUR,
                JOB_SETTINGS.VARIABLEHOURS,
                WORKINGDAY.DATE_WD,
                WORKINGDAY.H_BREAK,
                WORKINGDAY.H_REAL,
                WORKINGDAY.H_IN,
                WORKINGDAY.H_OUT
            FROM JOB, JOB_SETTINGS,WORKINGDAY
            WHERE JOB.ID_JOB = JOB_SETTINGS.ID_JOB
            AND JOB_SETTINGS.ID_JOB = WORKINGDAY.ID_JOB
            AND JOB.ID_JOB = 2")

在此处阅读更多内容enter link description here