1

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 Answers 2

1

$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();
Sign up to request clarification or add additional context in comments.

Comments

1

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;

1 Comment

Nit: die "Couldn't execute statement: " . $dbh->errstr; will never get executed because he's using RaiseError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.