sql - 如何在Delphi中正确翻译sql代码

标签 sql delphi firebird

在 sql-editor 中,此代码运行良好:

Select *
from Journal
where status<>'D' 
order by JDate,J_ID

但无法在 Delphi 中编写正确的代码。

1)

dstJournal.Close;
dstJournal.SQL.Clear;
dstJournal.SQL.CommaText:='Select * from Journal  order by JDate,J_ID';

sql error code=-104: Token Unknown J_ID

如果我只写“order by JDate”或只写“​​order by J_ID”,那么它就可以工作。

2) 如果我写:

dstJournal.SQL.CommaText:='Select * from Journal where Status<>"D" ';

it gets error:sql error code=-206:Column Unknown D.

我尝试了“D”,但 delphi 无法编译并显示“缺少运算符或分号”消息。

如何编写正确的delphi代码?

最佳答案

不要使用CommaText 。它用于将 SDF 格式(带引号、逗号分隔)的文本解析到 TStrings 对象中或从 TStrings 对象中解析出来。这不是你正在做的事情,所以不要使用它。

设置Text相反:

dstJournal.SQL.Text := 'SELECT * FROM Journal WHERE status <> ''D'' ORDER BY JDate, J_ID';

或者使用Add()方法:

dstJournal.SQL.Clear;
dstJournal.SQL.Add('SELECT * FROM Journal');
dstJournal.SQL.Add('WHERE status <> ''D''');
dstJournal.SQL.Add('ORDER BY JDate, J_ID');

I tried 'D' but delphi couldn't compile: missing operator or semicolon.

Delphi 字符串用单引号括起来,因此如果您打算将它们放入查询中,则必须使用两个单引号对其进行转义。

  dstJournal.SQL.Add('WHERE status <> ''D''');

关于sql - 如何在Delphi中正确翻译sql代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751452/

相关文章:

delphi - 即使 ApplyUpdates 位于 try 内,仍然会弹出错误消息...除了

delphi - 如何在Delphi XE8中处理TListBox一直向下滚动到最后一个TListBoxItem?

C# - Firebird获取更多错误信息

sql - 有没有办法在 Interbase firebird 的一个选择过程中拥有两个不同的 where 子句?

java - 如何在 sql 语句(Java)中使用大写的列名和表名?

mysql - 将多行合并为一行

delphi - 如何从 Delphi 获取/设置桌面图标位置和大小?

Firebird 缺少用户管理插件

c# - 如何显示varBinary字段数据

mysql - MySQL 中有没有一种方法可以计算一年中也已在前几年注册的成员数量?