TQuery中的delphi"无效使用关键字"
我正在尝试针对文件Journal.db使用以下TQuery的结果填充TDBGrid:
select * from Journal where Journal.where = "RainPump"
我尝试了Journal。" Where"和Journal。[Where]都无济于事。
我也尝试过:选择Journal。[Where]作为" Location"
,结果相同。
Journal.db是由第三方创建的文件,我无法更改字段名称。
问题是我感兴趣的字段称为"哪里",并且可以理解地导致上述错误。如何引用此字段而不会导致BDE(大概)爆炸?
解决方案
select * from Journal where Journal."where" = "RainPump"
像这样重写它,应该可以工作:
select * from Journal where Journal.[where] = "RainPump"
我,我将尴尬的一栏重命名。
我们可以将结果集插入具有"值"(不指定列名)的新表中,并在其中为新表提供自己的列名,然后使用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;
啊,我再次爱着delphi ...我找到了一种解决方法。 TQuery组件具有Filter属性:-)
我从查询中省略了" Where =" where子句,同时仍保留所有其他'and'条件。
我将Filter属性设置为" Where ='RainPump'"。
我将Filtered属性设置为True,生活又好了。
我仍然想知道是否有一种更聪明的方法来使用这项旧技术来执行此操作,但是如果它愚蠢并且有效,那么它就不是愚蠢的。
在MySQL中,表/列名称可以括在``(带角度的单引号)中。我不确定BDE允许什么,但是我们可以尝试将[where]替换为where
。
恐怕阅读此线程的人会感到BDE SQL引擎无法处理查询:
select * from Journal where Journal."Where" = "RainPump"
并且会浪费他们不必要的时间在周围闲逛。
实际上,这种构造效果很好。就像我们期望的那样," Where"周围的引号使BDE不能将其解释为关键字。
我不知道鲍德瑞克的特殊情况出了什么问题,或者他按什么顺序尝试了什么。他将问题描述为查询* .db表,但是他的SQL错误看起来更像是我们在直通模式下遇到的错误。或者,可能他简化了提交代码,从而消除了导致错误的真正原因。
我的测试使用:
BDE v.5.2(5.2.0.2)
Windows下的Paradox v.7(32b)
德尔福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
-Al