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
我认为这与原始错误有关?
提前致谢
Having a few problems with Ormlite and Android/Sqlite so any help appreciated. The database structure is
"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))"
The query is
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());
}
The following error is thrown
unrecognized token: "6B582835": , while compiling:
SELECT * FROM `jobTasks` WHERE
`JobID` = 6B582835-5A79-E011-A5E4-005056980009 AND `ParentJobTaskID` IS NULL )
ORDER BY `TaskOrder`
If I am passing the id as a string value why does it not show as such in the query?
The final error thrown is
java.sql.SQLException: Could not build prepared-query iterator for class
conx.Entities.JobParentTask
I assume this is related to the original error?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会羞愧地低下头。 JOBID 被关联为错误列的列名注释。
Will hang my head in shame. The JOBID was associated as a column name annotation for the wrong column.
呵呵。我怀疑您有一个现有的数据库表,您正在尝试将 Java 类与之匹配? Java 类中的
JobID
字段是否有可能不是字符串,而是偶然的整数?eq(JobParentTask.JOBID, id)
方法应该查看JobParentTask
内部的字段,并发现它是一个String
,需要逃脱了。这是 ORMLite 中的核心代码,已经运行了一段时间。您可以在 JobID 字段周围发布
JobParentTask
定义吗?如果您仍然遇到问题,立即的解决方法是使用“选择参数”:
:
类似这样的查询 构造使用了?并使用 SQL 注入参数。
Huh. I suspect you have an existing database table that you are trying to match a Java class against? Any chance that the
JobID
field in your Java class is not a string but is, by accident, an integer?The
eq(JobParentTask.JOBID, id)
method should look at the field inside ofJobParentTask
and see that it is aString
that needs to be escaped. This is core code in ORMLite that has been working for some time.Can you post the
JobParentTask
definition around the JobID field?If you still are having problems an immediate workaround is to use the "Select Args":
Something like:
Then the query that is constructed used a ? and injects the argument using SQL.