469,268 Members | 1,007 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Error of DBD::SQLite::db do failed: near ",": syntax error at C:\

47
Hi,
I have this error when i try to insert some values into the database via perl script.

there is my code
Expand|Select|Wrap|Line Numbers
  1. sub addWords {
  2.     my $self = shift;
  3.     my $wordsRef = shift;
  4.     my @words = \$wordsRef;
  5.     my $language = Language->new();
  6.     my $classId = undef;
  7.     my $wordId = undef;
  8.     if (!@words || !$language){
  9.         die "addWords invalid arguments.";
  10.     }
  11.     my $languageId = $language->id();
  12.     my $db = DBI->connect("dbi:SQLite:$dbFile","","",{RaiseError =>1, AutoCommit => 1, sqlite_unicode => 1}) or die "Unable to connect: $DBI::errstr\n"; 
  13.     foreach my $word (@words){ 
  14.         $db->do("INSERT INTO words(wordId, languageId, word, classId) VALUES ($wordId, $languageId, \"$word\", $classId);");         
  15.     }
  16.     $db->disconnect or warn "Disconnection failed: $DBI::errstr\n";;
  17. }
  18.  
Please help
Apr 21 '11 #1
3 6037
RonB
589 Expert Mod 512MB
Start by changing:
Expand|Select|Wrap|Line Numbers
  1. my @words = \$wordsRef;
To:
Expand|Select|Wrap|Line Numbers
  1. my @words = @$wordsRef;
Next, instead of the doing the 'do' statement, it would be better to do a prepare statement prior to the foreach loop and use a placeholder, then do an execute statement in the loop.
Expand|Select|Wrap|Line Numbers
  1.     my $sth = $db->prepare( "INSERT INTO words(wordId, languageId, word, classId) VALUES ($wordId, $languageId, ?, $classId)" );
  2.  
  3.     foreach my $word (@words) { 
  4.         $sth->execute($word);
  5.     }
Apr 21 '11 #2
Rabbit
12,516 Expert Mod 8TB
It's been a while but I'm pretty sure SQLite uses single quotes and not double quotes to demarcate strings.
Apr 21 '11 #3
miller
1,089 Expert 1GB
Winnie,

Address your first question please before asking a related one, or at least continue in the same thread:

http://bytes.com/topic/perl/answers/...trict-refs-use

- Miller
Apr 22 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Julien ARNOUX | last post: by
reply views Thread by tsivaraman | last post: by
10 posts views Thread by Luigi | last post: by
reply views Thread by kurtf | last post: by
2 posts views Thread by Hexade | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.