Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi guys, i am trying to write a simple utility to do the following: SELECT a bunch of lines from a remote database and INSERT into a local one. i seem to be running into some problems with the following relevant code sections:
while ( my (@log_entry) = $xlh->fetchrow_array ) { push(@log_entry, undef, "$_") ; $mlh->execute(@log_entry) ; last if $mch->err ; } sub get_log() { my $dbh = shift ; my $table = "log" ; my $field = join(',',@log_fields[0..6]) ; my $sth = $dbh->prepare( qq{ SELECT $field FROM $table LIMIT 25 } ) or db_err("Can't prepare statement: $DBI::errstr") ; $sth->execute or db_err("Can't execute statement: $DBI::errstr") ; return($sth) ; } sub put_log() { my $dbh = shift ; my $table = "log" ; my @field = @_ ; my ( $nph, $value_ph ) = "$#field" ; my $field = join(',',@field) ; while ( $nph-- > -1 ) { $value_ph .= '? ' ; } ; my $sth = $dbh->prepare( qq{ INSERT INTO $table ($field) VALUES ( $value_ph ) } ) or db_err("Can't prepare statement: $DBI::errstr") ; return($sth) ; }
i keep on getting the following error mesg: DBD::mysql::st execute failed: You have an error in your SQL syntax near 'NULL NULL NULL 'Login OK. someone pls help!!!

Replies are listed 'Best First'.
Re: db to db update
by dws (Chancellor) on Aug 27, 2002 at 22:57 UTC
    i keep on getting the following error mesg: DBD::mysql::st execute failed: You have an error in your SQL syntax near 'NULL NULL NULL 'Login OK.

    The query you're building has a syntax error in it. Figure out what the syntax error is, and fix it. That's pretty much all there is to it.

    One problem I see is the way you're building up a string of bind markers in put_log(). You need commas between them, not spaces.

    Hint: Depending on what RDBMS you're using (e.g., MySQL), prepare() won't catch syntax errors.

Re: db to db update
by abstracts (Hermit) on Aug 28, 2002 at 02:01 UTC
    Hello,

    Your $value_ph contains question marks with no commas to separate them. Try the following:

    my $field = join(',',@field); my $value_ph = join(',', map "?", @field);
    Hope this helps,,,