4

我正在尝试使用针对文件 Journal.db 的以下 TQuery 的结果填充 TDBGrid:

select * from Journal
where  Journal.where = "RainPump"

我都试过了Journal."Where"Journal.[Where]都没有用。

我也尝试过:select Journal.[Where] as "Location"结果相同。

Journal.db 是由第三方创建的文件,我无法更改字段名称。

问题是我感兴趣的领域被称为'where'并且可以理解地导致上述错误。如何在不导致 BDE(大概)爆炸的情况下引用此字段?

4

8 回答 8

4

啊,我又爱上了德尔福……我找到了解决方法。TQuery 组件具有 Filter 属性 :-)
我在查询中省略了“Where=” where 子句,同时仍保留所有其他“和”条件。
我将 Filter 属性设置为“Where = 'RainPump'”。
我将 Filtered 属性设置为 True,生活又恢复了。

我仍然想知道是否有更聪明的方法可以使用这种旧技术来做到这一点,但如果它很愚蠢并且有效,那么它并不愚蠢。

于 2008-09-23T13:28:19.823 回答
3

恐怕阅读此线程的人会产生 BDE SQL 引擎无法处理查询的印象:

select * from Journal where Journal."Where" = "RainPump"

并且会浪费他们的时间不必要地绕着它绕圈子。

事实上,这种结构工作得很好。正如您所期望的那样,“Where”周围的引号使 BDE 不会将其解释为关键字。

我不知道 Baldric 的特殊情况出了什么问题,或者他以什么顺序尝试了什么。他将问题描述为查询 *.db 表,但他的 SQL 错误看起来更像是您在直通模式下遇到的问题。或者,可能他简化了提交代码,从而消除了错误的真正原因。

我的测试使用:BDE v.5.2 (5.2.0.2) Paradox for Windows v. 7 (32b) Delphi 5.0 (5.62)

成功的声明的各种版本:

select * from Journal D0 where D0."Where" = "RainPump"
select * from Journal where Journal."Where" = "RainPump"
select * from ":common:Journal" D0 where D0."Where" = "RainPump"
select * from ":common:Journal" where ":common:Journal"."Where" = "RainPump"
select * from :common:Journal where Journal."Where" = "RainPump"
select * from ":common:Journal" D0 where D0."GUMPIK" = 3
select * from ":common:Journal" where ":common:Journal"."GUMPIK" = 3
select * from :common:Journal where Journal."GUMPIK" = 3

看起来正确但因“无效使用关键字”而失败的语句版本:

select * from ":common:Journal" where :common:Journal."Where" = "RainPump"
select * from :common:Journal where :common:Journal."Where" = "RainPump"
select * from ":common:Journal" where :common:Journal."GUMPIK" = 3
select * from :common:Journal where :common:Journal."GUMPIK" = 3

-阿尔。

于 2008-12-29T17:47:37.820 回答
2

像这样重写它,应该可以工作:

select * from Journal where Journal.[where] = "RainPump"
于 2008-09-23T11:56:42.040 回答
2

您可以将结果集插入带有“值”(不指定列名)的新表中,您在新表中给出了自己的列名,然后使用 TQuery 从该表中进行选择,例如:

Query1.sql.clear;
query1,sql.add('Insert into newtable values (select * from Journal);');
query1.sql.add('Select * from newtable where newcolumn = "Rainpump";');
query1.open;
于 2008-09-23T12:44:49.897 回答
0
select * from Journal where Journal."where" = "RainPump"
于 2008-09-23T11:56:37.437 回答
0

我,我会重命名尴尬的专栏。

于 2008-09-23T12:21:32.480 回答
0

在 MySQL 中,表/列名可以用 `` (带角度的单引号)括起来。我不确定 BDE 允许什么,但您可以尝试将 [where] 替换为 `where`

于 2008-09-23T15:32:56.327 回答
0

好的,所以在任何 SQL 系统中以键盘命名列都是不好的。您会将列命名为“select”或“count”或“alter”或“table”,或者只是为了取乐它“truncate”或“drop”?我希望不会。

即使你在这个例子中建立了工作,你也在为任何追随你的人创造一个雷区。照mj2008所说的去做,把血腥的列重命名。

允许此列名称保留是构建系统并将您列入任何项目经理的便便列表的最糟糕示例。

于 2011-11-06T19:43:26.777 回答