1
votes

I am trying to run a select statement to a sqlite3 database using Perl DBI. The following is the code:

my $dbh = DBI->connect( "DBI:SQLite:dbname=./GenBankData.db" , "" , "" , { PrintError => 0 , RaiseError => 1 } );
    my $Sql = 'select AccessionQueryResultID, AccessionNumber, Definition from AccessionQueryResult';
    my $sth = $dbh->prepare( $Sql )  or die "Couldn't prepare statement: " . $dbh->errstr;;
    $sth->execute( $Sql) or die "Couldn't execute statement: " . $dbh->errstr;

But I am getting the following error: DBD::SQLite::st execute failed: called with 1 bind variables when 0 are needed at /home/mysite.cgi line 33

I have checked that the database and table exist and the same query works fine if I used the sqlite3 command line to run the query.

Thanks

2

2 Answers

1
votes

$sth is a statement handle. It represents the SQL statement/query, so having to provide the SQL statement to the object again makes no sense.

The args $sth->execute expects are values needed to fill in replaceable parameters (?) in the query.

my $sth = $dbh->prepare("INSERT INTO MyTable VALUES (?, ?)");
$sth->execute(1, "a");
$sth->execute(2, "b");
$sth->execute(3, "c");

In your case, since your query doesn't use any replaceable parameters, you should not pass any args to execute.

$sth->execute();
1
votes

You're calling the execute command with incorrect parameters. You've already setup the SQL statement in the previous line, you don't need to do it again. Try this instead:

$sth->execute() or die "Couldn't execute statement: " . $dbh->errstr;