简体   繁体   English

如何在Delphi中的SQL中使用节中的变量

[英]how to use a variable in a section into SQL from delphi

source:= tabledemap.FieldByName('table source').AsString + '('+ tabledemap.FieldByName('colonne source').AsString+')';
  showmessage(source)  ;

The source value is the column: a.name (firsatname) I want to insert in this column the string 't' But the problem I want to use the value of the source variable in a sql request like this 源值是列:a.name(firsatname)我想在此列中插入字符串“ t”,但是我想在这样的sql请求中使用源变量的值的问题

 FDQuery6.SQL.Add ('INSERT INTO  source  Values ( "t" ) ');

     FDQuery6.Execute;

      showmessage('row inserted');

But the program fails to use the text of the value of the source variable and think that source is a name of a table and displays to me 但是程序无法使用源变量值的文本,并认为源是表的名称并向我显示

---------------------------
Notification des exceptions du débogueur
---------------------------
Le projet x a déclenché la classe d'exception EMySQLNativeException avec le message '[FireDAC][Phys][MySQL] Table 'a.source' doesn't exist'.

I have tried "source" , +source+ but it won't work also . 我已经尝试过“ source”,+ source +,但也无法使用。 If anyone can help me to use the text of the source variable in the SQL query I will be grateful. 如果有人可以帮助我在SQL查询中使用源变量的文本,我将不胜感激。

In FireDAC you can paremetrize table and field names by using preprocessor macros. 在FireDAC中,您可以使用预处理器宏来解析表和字段名称。 For example: 例如:

FDQuery.SQL.Text := 'INSERT INTO &TableName (&FieldName) VALUES "ConstValue"';
FDQuery.MacroByName('TableName').AsIdentifier := 'MyTable';
FDQuery.MacroByName('FieldName').AsIdentifier := 'MyField';
FDQuery.ExecSQL;

For details about this kind of macros, see the substitution variables topic. 有关此类宏的详细信息,请参见替代变量主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM