Mysql准备语句多个插入

时间:2014-03-21 05:26:36

标签: mysql

我一直在尝试使用mysql prepare语句插入多个插入条目。但是,它没有发生。

PREPARE stmt1 FROM 
'Insert into bookingdetails 
VALUES
(?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5),
(?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5)';
SET @a = 4;
EXECUTE stmt1 USING @a;
DEALLOCATE PREPARE stmt1;

结果是

PREPARE stmt1 FROM  'Insert into bookingdetails  VALUES (?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5), (?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5)'

0 row(s) affected Statement prepared.

请建议。

上述问题已得到解决。我有另一个。

流量。

我正在从PHP传递一个插入查询。

Insert into bm VALUES(?,'2014-03-11')

根据PREPARE STATEMENT友好格式化上述插入。 var_bm变量是stored_procedure的IN参数,其IN值是从PHP插入的。

set xbmquery=(select concat("'",replace(var_bm,"'", "\\'"),"\'\;"));

Output
'Insert into bm VALUES(?,\'2014-03-11\')';

以下是存储过程中的prepare语句。

PREPARE stmt1 FROM 'Select xbmquery';
SET @a = 16;
EXECUTE stmt1 USING @a;
DEALLOCATE PREPARE stmt1;

数据未插入相应的表中。 请让我知道我的错误。

3 个答案:

答案 0 :(得分:2)

您有两个占位符,因此您必须使用USING传递两个值

EXECUTE stmt1 USING @a, @a;
                    ^^^^^^

这是 SQLFiddle 演示

答案 1 :(得分:1)

你必须绕过作为其他字符串一部分的字符串的单引号 您还错过了第二个参数的值。

更改:

PREPARE stmt1 FROM 
'Insert into bookingdetails 
VALUES
(?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5),
(?,''2014-03-12'',''sl1'',3,''GA'',12.5,37.5)';
SET @a = 4;
EXECUTE stmt1 USING @a;
DEALLOCATE PREPARE stmt1;

致:

PREPARE stmt1 FROM 
'Insert into bookingdetails 
VALUES
(?,\'2014-03-12\',\'sl1\',3,\'GA\',12.5,37.5),
(?,\'2014-03-12\',\'sl1\',3,\'GA\',12.5,37.5)';
SET @a = 4;
EXECUTE stmt1 USING @a, @a;
DEALLOCATE PREPARE stmt1;

答案 2 :(得分:0)

尝试以下使用单引号的代码

PREPARE stmt1 FROM 
'Insert into bookingdetails 
VALUES
(?,'2014-03-12','sl1',3,'GA',12.5,37.5),
(?,'2014-03-12','sl1',3,'GA',12.5,37.5)';
SET @a = 4;
EXECUTE stmt1 USING @a, @a;
DEALLOCATE PREPARE stmt1;