在mysql中运行多个SQL命令

时间:2017-01-27 14:02:25

标签: mysql delphi

我试图用Delphi 7编写的代码如下。但是我无法在一次操作中运行多个MySQL命令。 我想;

A- customer table >>> address_id, customer_id, firstname, lastname,
B- address table  >>> address_id, customer_id, firstname, lastname
C- customer table >>> address_id,

B = A and C = B

我需要以。的形式编码。 我如何需要mysql代码?

modul.Q_Customer.Close;
modul.Q_Customer.SQL.Clear;
modul.Q_Customer.SQL.Add('INSERT INTO customer (customer_group_id,firstname, lastname, email, approved, ip, model, telephone, fax, status, date_added)');
modul.Q_Customer.SQL.Add('Values(:CG, :ADI, :SOYADI, :MAIL, "1", "127.0.0.1", "ISYERI", :TELEFON, :FAX, "1", NOW()); ');

modul.Q_Customer.SQL.Add('INSERT INTO address (customer_id, firstname,lastname,company,VergiDaire, address_1, address_2, city, postcode, country_id, zone_id, VergiNo)');
modul.Q_Customer.SQL.Add('Values(LAST_INSERT_ID(), :ADI, :SOYADI, :FIRMA, :VD, :ADRES1, :ADRES2, :SEMT, :POSTAKOD, "215", :IL, :VN; ');

modul.Q_Customer.SQL.Add('UPDATE customer SET address_id = LAST_INSERT_ID() WHERE customer_id = LAST_INSERT_ID();');

modul.Q_Customer.ParamByName('CG').Value:=CG;
modul.Q_Customer.ParamByName('ADI').Value:=ADI;
modul.Q_Customer.ParamByName('SOYADI').Value:=SOYADI;
modul.Q_Customer.ParamByName('MAIL').Value:=MAIL;
modul.Q_Customer.ParamByName('TELEFON').Value:=TELEFON;
modul.Q_Customer.ParamByName('FAX').Value:=FAX;
modul.Q_Customer.ParamByName('FIRMA').Value:=UNVAN;
modul.Q_Customer.ParamByName('ADRES1').Value:=ADRES1;
modul.Q_Customer.ParamByName('ADRES2').Value:=ADRES2;
modul.Q_Customer.ParamByName('SEMT').Value:=SEMT;
modul.Q_Customer.ParamByName('POSTAKOD').Value:=POSTAKOD;
modul.Q_Customer.ParamByName('IL').Value:=IL;
modul.Q_Customer.ParamByName('VD').Value:=VergiD;
modul.Q_Customer.ParamByName('VN').Value:=VergiN;
modul.Q_Customer.ExecSQL;

2 个答案:

答案 0 :(得分:0)

我认为你的行

modul.Q_Customer.SQL.Add('Values(LAST_INSERT_ID(), :ADI, :SOYADI, :FIRMA, :VD, :ADRES1, :ADRES2, :SEMT, :POSTAKOD, "215", :IL, :VN; ');

后缺少结束括号

VN;

顺便说一下,使用工具lkie MySQl Workbench尝试识别这样的问题总是最好。

答案 1 :(得分:0)

如果使用ZEOS组件,可以通过以下方式完成:

function DoMySQLScript(Script: string; Args: array of const;
  Delimiter: string): boolean;
var ZSqlProcessor: TZSQLProcessor;
begin
  Result := False;
  Script := FormatQuery(Script, Args); // Format the Script with Args
  ZSqlProcessor:= TZSQLProcessor.Create(nil);
  try
      ZSqlProcessor.Delimiter := Delimiter;
      ZSqlProcessor.Script.Text := Script;
      ZSqlProcessor.Connection:= MyConnection; // the connection to you DB
      try
         ZSqlProcessor.Execute;
         Result := True;
      except
        Result := False;
      end;
  finally
    ZSqlProcessor.free;
  end;
end;

在你的程序中,你可以这样做:

begin
    S:= 'INSERT INTO customer (customer_group_id,firstname, lastname, email, approved, ip, model, telephone, fax, status, date_added)'
    'Values(%s, %s, %s, %s, "1", "127.0.0.1", "ISYERI", %s, %s, "1", NOW());'
    +' INSERT INTO address (customer_id, firstname,lastname,company,VergiDaire, address_1, address_2, city, postcode, country_id, zone_id, 
    +' VergiNo) Values(LAST_INSERT_ID(), %s, %s, %s, %s, %s, %s, %s, %s, "215", %s, %s;'
    +'UPDATE customer SET address_id = LAST_INSERT_ID() WHERE customer_id = LAST_INSERT_ID();

    DoMySQLScript(S, [CG,ADI,SOYADI,MAIL,TELEFON,FAX,UNVAN,ADRES1,ADRES2,SEMT,POSTAKOD,IL,VergiD,VergiN], ';');
end;

注意:您可以在ZSqlProcessor.pas

中找到ZSqlProcessor