mysqldelphidbexpress

How to send several insert statements using TSQLConnection.Execute


I'm using TSQLConnection to connect to a remote MySql DB.

I want to send several INSERT statements at one time, instead of sending one by one (this would be painfully slow).

But, when I try to do this, I get an error from the server.

If I send only one statement, like this, it works:

mySQLConnection.Execute('insert into table values (1,100)',nil);

But, if I send like this:

mySQLConnection.Execute('insert into table values (1,100);insert into table values (2,200);insert into table values (3,300)',nil);

The server will raise an exception:

Project xxx.exe raised exception class TDBXError with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...

Is there any way to make this work? On MySQL Workbench, the same SQL statement with several INSERTs will work, but not in Delphi.


Solution

  • Each call to .execute() can only execute one query. If you want to insert multiple rows, put more than one list of values after VALUES.

    mySQLConnection.execute('insert into table values (1,100), (2,200), (3,300)',nil);