问题
I would like to insert values into two separate MySQL tables using DBI. I tried to combine two working INSERT
queries into one by inserting a ;
between them:
$dbh->do(q{
INSERT INTO `testA`(test) values('testvalue111');
INSERT INTO `testB`(test) values('testvalue222');
});
But I always get an error:
Syntactic error in "INSERT INTO `testB`(test) values('testvalue222 ..."
If I separate the queries into two separate do
calls, it works. But the combined query works fine in phpMyAdmin. Why does it fail in Perl?
回答1:
You need to set an option in the connect
call to enable this. From the docs:
mysql_multi_statements
As of MySQL 4.1, support for multiple statements seperated by a semicolon (;) may be enabled by using this option. Enabling this option may cause problems if server-side prepared statements are also enabled.
It is disabled by default, and should probably remain unset - that's a large avenue for SQL injection (which you should be reading about especially if you're not using binds).
回答2:
By default, DBD::mysql doesn't allow you to execute multiple statements at once. Although you can enable this with the mysql_multi_statements
option as Mat showed, you shouldn't do that: among other things, your code will be less portable because some database drivers don't have any such option, and it can cause issues with prepared statements.
Instead, just issue a separate DBI command for each statement.
回答3:
INSERT INTO testA
(test) values('testvalue111'),('testvalue222');
来源:https://stackoverflow.com/questions/13333230/why-doesnt-joining-multiple-mysql-queries-with-semicolons-work-with-perl-dbi