简体   繁体   中英

Inserting an array with single quotes(') in database with perl DBI

I am trying to insert an array into database and my query is as below:

DBD::mysql::st execute failed: called with 8 bind variables when 3 are needed at main.pl line 114, line 7.

my $sql_in = "INSERT INTO $table VALUES(?,?,?,'','','','')";
my $stmt_in = $dbh->prepare($sql_in) or die "Couldn't prepare statement: ".$dbh->errstr;
$stmt_in->execute($q_num,$datatype,@qq) or die "Couldn't execute statement:".$stmt_in->errstr;
$stmt_in->finish;

@qq contains below test:

Reporting that one of @its many problems had been the recent@ extended sales slump in women's apparel, the seven-store retailer said it would start a three-month liquidation sale in all of its stores. (A) its many problems had been the recent (B) its many problems has been the recently (C) its many problems is the recently (D) their many problems is the recent (E) their many problems had been the recent

Each SQL parameter ( ? ) must receive exactly one value when you execute the query. The array @qq contains multiple values. Based on the error message, the array must contain six elements.

Looking at your query, I'm guessing that you want the entire content of the array to go into a single field. If so, you should be able to use

$stmt_in->execute($q_num,$datatype, join("\n", @qq)) or die...

to achieve this, possibly replacing the \\n with another character/string that you want inserted between the array's values.

Alternately, if you want to insert only the first element in the array (which could be the case since the question only shows a single element), use

$stmt_in->execute($q_num,$datatype,$qq[0]) or die...

If you want to insert all array elements with each element going into a separate field in the table, you need to construct a query with the right number of placeholders, most likely by using join(', ', ('?') x @qq) if the array's size is variable.

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