简体   繁体   中英

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:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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