简体   繁体   English

我可以使用 delphi edit 作为名称在 sql 中创建表吗

[英]Can I create table in sql using delphi edit as a name

In my program I would like to create a new table whenever I click a button.在我的程序中,每当我单击一个按钮时,我都想创建一个新表。 Table name is supposed to be in the Edit.Text .表名应该在Edit.Text中。 I made it this way:我是这样做的:

procedure TRyby_Form01.WyjazdyBTNClick(Sender: TObject);
var Wyjazd  : string; 
begin
  Wyjazd := WyjazdEDT.text;      // edit wchich is supposed to be table name
  with wyjazdquery3 do

    begin
      parameters.ParamByName('wyjazd').value := wyjazd;
    end;

    wyjazdquery3.ExecSQL;

    with wyjazdquery4 do
    begin
      parameters.ParamByName(':wyjazd').value := wyjazd;
    end;

    wyjazdquery4.ExecSQL;
    wyjazdquery5.ExecSQL;
  end;

end.

I have created parameter as a string in a query and made this syntax我在查询中将参数创建为字符串并制作了此语法

Create Table :wyjazd (
Ryba char(20),
Miara int,
Waga decimal(6,3),
Data date,
przyneta char(60),
metoda char (20) );

Unfortunately when I click the button, all I receive is "incorrect syntax near @P1".不幸的是,当我单击该按钮时,我收到的只是“@P1 附近的语法错误”。 Is it possible to make it the way I tried or SQL doesn't allow it?是否可以按照我尝试的方式进行或 SQL 不允许?

Easiest way is to use Format with %s for Table name.最简单的方法是使用带有 %s 的格式作为表名。 Simply said - you cant change structure of SQL with parameters.简单地说 - 你不能用参数改变 SQL 的结构。 Use this string as template for Query.SQL:使用此字符串作为 Query.SQL 的模板:

Create Table %s (
Ryba char(20),
Miara int,
Waga decimal(6,3),
Data date,
przyneta char(60),
metoda char (20) );

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

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