0

I want to insert values into Sqlite3 table using Perl DBI. I was able to insert hard coded values without any problem. When I tried to use perl variables, then I get an error "DBD::SQLite::db do failed: no such column:"

This works:

$dbh->do("insert into Gene values (12, 'AAAAAA', 66, 86, 76)");

But this code

$dbh->do("INSERT INTO Gene values (NULL, $sequence, $siteNumber, $begin, $length)");

throws the error

DBD::SQLite::db do failed: no such column
3
  • 4
    Don't say "I got an error". Always say "Here is the error I got" and then show us the exact error. Don't paraphrase it. Don't retype it. Cut & paste the error message exactly from your screen. Commented Mar 6, 2013 at 2:01
  • "DBD::SQLite::db do failed: no such column" is the error I got. Commented Mar 6, 2013 at 2:04
  • 2
    The first thing you want to do to debug this is to print "INSERT INTO Gene values (NULL, $sequence, $siteNumber, $begin, $length)"; so you know what SQL command is being executed. We can't magically tell what the values of those 4 variables are. Commented Mar 6, 2013 at 2:05

2 Answers 2

8

You should always prepare and execute your SQL statements and use placeholders for variable values as a matter of course.

Try this code

my $sth = $dbh->prepare('INSERT INTO Gene VALUES (?, ?, ?, ?, ?)');
$sth->execute(undef, $sequence, $siteNumber, $begin, $length);
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem will have been at $sequence, which required quoting to be a valid SQL string literal in the insert statement. As a result it thought you were trying to refer to a column called AAAAAA, and not the string 'AAAAAA' that you intended. Perl's string interpolation doesn't add the quotes for you.

Placeholders and $dbh->prepapre are by far the most robust solution here though (for example, what to do if your string contains the quote character ' ? $dbh->prepare already has that coded correctly).

Comments

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.