Ormlite查询帮助新手

时间:2011-07-21 04:39:14

标签: ormlite

对Ormlite和Android / Sqlite有一些问题,所以任何帮助表示赞赏。数据库结构是

"CREATE TABLE [jobTasks](" +
        "[JobTaskID] varchar(50) NOT NULL PRIMARY KEY DEFAULT (newid())," +
        "[JobID] varchar(50) NOT NULL," +
        "[EnumTaskType] Real," +
        "[TaskID] varchar(50)," +
        "[TaskOrder] SmallInt," +
        "[TaskDescription] NVarChar (50) ," +
        "[EnumTaskStatus] Real," +
        "[EnumTaskQualifier] Real," +
        "[IsTaskQualifierForced] Bit," +
        "[CompletedBy] NVarChar (50)," +
        "[CompletedOn] DateTime," +
        "[CompletedOnOffset] NVarChar (6)," +
        "[JobAssetID] varchar(50),[wadtDeleted] Bit NOT NULL DEFAULT ((0))," +
        "[WadtModifiedBy] NVarChar (15)," +
        "[WadtModifiedOn] DateTime," +
        "[WadtModifiedOnDTOffset] NVarChar (6)," +
        "[WadtRowID] varchar(50) DEFAULT (newid())," +
        "[ParentJobTaskID] varchar(50)," +
        "[Rowguid] varchar(50) NOT NULL DEFAULT (newid())," +
        "SyncDeleted Bit NOT NULL DEFAULT ((0))," +
        "FOREIGN KEY(JobID) REFERENCES jobJob(jobID)," +
        "FOREIGN KEY(enumTaskType)  REFERENCES enumTaskType(enumTaskType)," +
        "FOREIGN KEY(TaskID) REFERENCES cfgWorkFlowTasks(TaskID)," +
        "FOREIGN KEY(enumTaskStatus) REFERENCES enumTaskStatus(enumTaskStatus))"

查询

QueryBuilder<JobParentTask, String> qb =
    _databaseContext.JobParentTasks().queryBuilder();
qb.orderBy(JobParentTask.TASKORDER, true);
qb.where().eq(JobParentTask.JOBID, id).and()
     .isNull(JobParentTask.PARENTJOBTASKID);
PreparedQuery<JobParentTask> preparedQuery = queryBuilder.prepare();
CloseableIterator<JobParentTask> iterator
    =_databaseContext.JobParentTasks().iterator(jobParentTaskPreparedQuery);
while (iterator.hasNext()) {
    parentTasks.add(iterator.next());
}

抛出以下错误

 unrecognized token: "6B582835": , while compiling:
 SELECT * FROM `jobTasks` WHERE
 `JobID` = 6B582835-5A79-E011-A5E4-005056980009 AND `ParentJobTaskID` IS NULL )
 ORDER BY `TaskOrder` 

如果我将id作为字符串值传递,为什么它在查询中不显示?

抛出的最终错误是

java.sql.SQLException: Could not build prepared-query iterator for class
conx.Entities.JobParentTask

我认为这与原始错误有关?

提前致谢

2 个答案:

答案 0 :(得分:1)

将我的头埋在耻辱中。 JOBID与错误列的列名注释相关联。

答案 1 :(得分:0)

咦。我怀疑你有一个现有的数据库表,你试图匹配Java类?您的Java类中的JobID字段是否字符串,但偶然是整数?

eq(JobParentTask.JOBID, id)方法应该查看JobParentTask内的字段,看看它是String需要转义的内容。这是ORMLite的核心代码,已经有一段时间了。

您可以在JobID字段周围发布JobParentTask定义吗?

如果您仍然遇到问题,立即解决方法是使用“选择Args”:

  

http://ormlite.com/docs/select-arg

类似的东西:

SelectArg jobIdArg = new SelectArg(id);
queryBuilder.where().eq(JobParentTask.JOBID, jobIdArg).and()...

然后构造的查询使用了?并使用SQL注入参数。