SUSH
SUSH

Reputation: 91

How can I bind the values of the parameter in the SQL statement using Perl?

HI am using SQLite DB and whenever I try to bind the values of parameter into the statement I am getting an error.

Here's the piece of code:

my $sth = $dbh->prepare("SELECT UserId,UserName,CardNo,GroupId,Role,VerifyType FROM
    UsersList limit ?,? "); 
$sth->bind_param(1, undef,SQL_VARCHAR);
$sth->bind_param(2, undef,SQL_VARCHAR);
$sth->execute($page,$results_per_page);

Here's the error:

ERROR: DBD::SQLite::st execute failed: datatype mismatch at line 68.

Can anybody Please help me out and let me know what datatype am I supposed to put in place of SQL_VARCHAR?

Upvotes: 1

Views: 1129

Answers (3)

J. Ceron
J. Ceron

Reputation: 1345

my $sql = "INSERT INTO table 
                       (name, age)
           VALUES 
                       ('$name', ?);

$sth = $dbh->prepare($sql);

#if not $age insert NULL value 
$sth->execute($age ? $age : undef); 

Upvotes: 0

Øyvind Skaar
Øyvind Skaar

Reputation: 2328

Looks like you are using bind_param() wrong. Take a look at this example:

use DBI qw(:sql_types);  # Don't forget this

my $sth = $dbh->prepare(q{
    SELECT bar FROM foo GROUP BY bar HAVING count(*) > ?;
});
$sth->bind_param(1, 5, SQL_INTEGER);
$sth->execute();

i.e.: no arguments to execute() and the value goes in the bind_param() call.

Upvotes: 5

Donal Fellows
Donal Fellows

Reputation: 137567

Limits are integers and not text, so I'd guess SQL_INTEGER might be a thing to try first?

Upvotes: 1

Related Questions