简体   繁体   English

在mysql中使用prepared-statement插入查询?

[英]Insert query using prepared-statement in mysql?

I have an stored procedure like this: 我有一个这样的存储过程:

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',p1, p2,p3,')');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

But when I want to run it, it has this error: 但是当我想运行它时,它有这个错误:

> Procedure execution failed 1136 - Column count doesn't match value
> count at row 1

and another thing is that when I just run the insert code it runs! 另一件事是,当我运行插入代码时,它运行!

plz help me. 请帮助我。

With all due respect, the way you do it kindof defeats the whole purpose of prepared statements. 尽管如此,你所采用的方式有点违背准备好的陈述的全部目的。 I'd use this form instead: 我会改用这个表格:

SET @query = 'INSERT INTO tblcommodity (id, idname, count) VALUES (?, ?, ?)';
PREPARE stmt FROM @query;
EXECUTE stmt USING @p1, @p2, @p3;

它应该是

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',',',p1,',', p2,',',p3,')');

Try this: 尝试这个:

SET @query = CONCAT("insert into tblcommodity (id , idname , count) values (", p1, ", '", p2,"',",p3,")"); 
PREPARE stmt FROM @query; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt;

BTW, Your code: BTW,您的代码:

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',p1, p2,p3,')');

has an starting comma: 有一个起始逗号:

    ,p1,p2,p3  

it means FOUR fields in the statement. 它表示声明中的四个字段。 Thats why you get an "Column count doesn't match value" 这就是为什么你得到“列数与值不匹配”

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

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