in reply to Quoting in DBI sql query for on the fly query

my $sqlstatement = q{ SELECT Table1.Name, Table1.Address, Table1.City, Table1.State, Table1.ZIP, Table1.Phone FROM Table1 WHERE Table1.BusType = ?}; my $sth = $dbh->prepare($sqlstatement); foreach my $line (<DATA>) { chomp $line; $sth->execute( $line ); # your fetch here fetch }
also see DBI's quote function. DBI
Boris

Replies are listed 'Best First'.
Re^2: Quoting in DBI sql query for on the fly query
by ParisR (Acolyte) on Oct 29, 2004 at 02:08 UTC
    I've tried, but something still is not working.
    Here is the code:
    #! perl -w use DBI; use DBD::ODBC; # open connection to Access database $dbh = DBI->connect('dbi:ODBC:fred3'); my @lines = <DATA>; my $line; foreach $line ( @lines ) { my $sqlstatement = q{ SELECT Table1.Name, Table1.Address, Table1.City +, Table1.State, Table1.ZIP, Table1.Phone FROM Table1 WHERE Table1.Bus +Type = ?}; my $sth = $dbh->prepare($sqlstatement); foreach my $line (<DATA>) { chomp $line; $sth->execute( $line ); my $newdir= 'c:\redone'; my $extension= ".txt"; my $target = "$newdir/$line"; open( FINAL, ">$target" ) or die "Can't open $target:$!"; #output database results while (@row=$sth->fetchrow_array) { print FINAL "$row[0]\n$row[1]\n$row[2] $row[3] $row[4]<br>\n$row[5]\n\ +n"} close FINAL; } # end of fetchrow_array } # end of loop for each of data below __DATA__ ARCHITECTS INVESTIGATORS
      What is not working exactly? I've noted a couple of areas that may be giving trouble
      .... ### my $newdir= 'c:\redone'; my $newdir= 'c:/redone'; my $extension= ".txt"; ## I would be tempted to take the preceeding 2 statements out of the l +oop my $target = "$newdir/$line"; open( FINAL, ">$target" ) or die "Can't open $target:$!"; #output database results ### while (@row=$sth->fetchrow_array) { while (@row=$sth->fetchrow_array()) { ...
      something still is not working

      Could you please be a little less specific? I'm practicing for my mindreading act. Thanks.

      You write foreach my $line (<DATA>) {, but at that point, you've already read in all of <DATA>, to @lines. What's more, you've already opened a loop over @lines -- foreach $line ( @lines ) {. Finally, you only close one of these two loops.

      You need to use strict; and pay attention to what perl tells you when the code fails.