Ormlite 新手查询帮助

发布于 2024-11-25 06:26:45 字数 2313 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-12-02 06:26:45

我会羞愧地低下头。 JOBID 被关联为错误列的列名注释。

Will hang my head in shame. The JOBID was associated as a column name annotation for the wrong column.

时间海 2024-12-02 06:26:45

呵呵。我怀疑您有一个现有的数据库表,您正在尝试将 Java 类与之匹配? Java 类中的 JobID 字段是否有可能不是字符串,而是偶然的整数?

eq(JobParentTask.JOBID, id) 方法应该查看 JobParentTask 内部的字段,并发现它是一个 String ,需要逃脱了。这是 ORMLite 中的核心代码,已经运行了一段时间。

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

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

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

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

类似这样的查询 构造使用了?并使用 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 of JobParentTask and see that it is a String 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":

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

Something like:

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

Then the query that is constructed used a ? and injects the argument using SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文