gpt4 book ai didi

delphi - TFDQuery 和 SQLite : Type mismatch for field, 期望:LargeInt 实际:WideString

转载 作者:行者123 更新时间:2023-12-02 05:46:46 27 4
gpt4 key购买 nike

使用 Delphi 10.2、SQLite 和 Teecharts。我的 SQLite 数据库有两个字段,创建方式为:

CREATE TABLE HistoryRuntime ('DayTime' DateTime, Device1 INTEGER DEFAULT (0));

我使用名为 qryGrpahRuntimeTFDQuery 和以下 SQL 访问该表:

SELECT DayTime AS TheDate, Sum(Device1) As DeviceTotal
FROM HistoryRuntime
WHERE (DayTime >= "2017-06-01") and (DayTime <= "2017-06-26")
Group by Date(DayTime)

使用 Delphi IDE 中的字段编辑器,我可以添加两个持久字段,获取 TheDate 作为 TDateTimeFieldDeviceTotal 作为 TLargeIntField

我在程序中运行此查询来创建 TeeChart,它是我在设计时创建的。只要查询返回一些记录,这一切都有效。但是,如果没有请求日期的记录,我会收到 EDatabaseError 异常,并显示以下消息:

qryGrpahRuntime: Type mismatch for field 'DeviceTotal', expecting: LargeInt actual: Widestring

我在网上搜索了大量关于如何防止空查询上出现此错误的解决方案,但没有找到任何结果。据我所知,当没有返回数据时,SQLite 默认使用宽字符串字段。我尝试在查询中使用 CAST,但似乎没有任何区别。

如果我删除持久字段,查询将在空返回集上毫无问题地打开。但是,为了在 IDE 中使用 TeeChart 编辑器,我似乎需要持久字段。

有没有办法让我可以使用持久字段来完成这项工作,或者我是否必须丢弃持久字段,然后在运行时添加 TeeChart 系列?

最佳答案

Adjusting FireDAC Mapping 中描述了此行为FireDAC 的 SQLite 手册章节:

For an expression in a SELECT list, SQLite avoids type name information. When the result set is not empty, FireDAC uses the value data types from the first record. When empty, FireDAC describes those columns as dtWideString. To explicitly specify the column data type, append ::<type name> to the column alias:

SELECT count(*) as "cnt::INT" FROM mytab

所以修改你的命令,例如这样(我使用了BIGINT,但您可以使用任何映射到 64 位有符号整数数据类型且不自动递增的 pseudo data type ,这对应于您的持久 TLargeIntField 字段):

SELECT
DayTime AS "TheDate",
Sum(Device1) AS "DeviceTotal::BIGINT"
FROM
HistoryRuntime
WHERE
DayTime BETWEEN {d 2017-06-01} AND {d 2017-06-26}
GROUP BY
Date(DayTime)

附注我使用 BETWEEN 做了一个小的优化运算符(仅计算列值一次),并使用 escape sequence对于日期常量(我猜,实际上你用参数替换了它;所以只是出于好奇)。

<小时/>

此数据类型提示由 FDSQLiteTypeName2ADDataType 解析在其 AColName 参数中获取并解析格式为 :: 的列名称的过程。

关于delphi - TFDQuery 和 SQLite : Type mismatch for field, 期望:LargeInt 实际:WideString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com