develooper Front page | perl.beginners | Postings from March 2002

RE: Connecting to DB

Thread Next
From:
Crook, Richard W
Date:
March 28, 2002 13:54
Subject:
RE: Connecting to DB
Message ID:
359580FB3FE6D311A7FC00508B94D73C07FDD945@potomac.mis.earthlink.net
Looks like you need to declare my $dbh, my $action, my $passwd up where
you've declared the others or just turn off strict. 
To debug your database statement try: die $action to see what the script is
sending to the database

-----Original Message-----
From: Michael Gargiullo [mailto:gargiullo@comcast.net]
Sent: Thursday, March 28, 2002 1:31 PM
To: Beginners
Subject: FW: Connecting to DB


OK I have the following array

@new which has the following data in it
	this town
	this state
	zipcode
	county
	fips (I want discarded)
	Area code

And want to insert these vars into my DB

I'm using this code:


#!/usr/bin/perl -w
use LWP::Simple;
use DBI;
use strict;
my $db="zipcodes";              #############
my $user="notshown";            # Database  #
my $pass="notshown";            # Variables #
my $tablename="zips";           #############
my $insertToDB=1; #Change to zero if you don't want to write to DB
my $printToTerm=1; #Change to zero if you don't want results printed to
the terminal
my $content;
my @arr;
my @new;
my $line;
my $count=0;

$_=get("http://zipinfo.com/cgi-local/zipsrch.exe?cnty=cnty&ac=ac&zip=08550")
;#
Get Zipcode Data
@arr = /<td align=center>(.*?)<\/td>/g; # Stript Most HTML
shift(@arr); #Remove Unwanted
shift(@arr); #Data Returned
# Strip trailing </font> tag, remove fips code, and Store in array
foreach $line(@arr){
        $_=$line;
        /(.*.)<(.....)/;
        if($count != 6){
        $new[$count]=$1;
        ++$count;
        }
}
# Insert Data into Database MS SQL SERVER 2K
if($insertToDB==1){
        $dbh = DBI->connect('DBI:Sybase:server=192.168.0.2', $user,$passwd);
        $dbh->do(INSERT into
$tablename(city,state,county,zip,areacode)VALUES($new[0],$new[1],$new[3],$ne
w[2],$new[5]);
        $action->execute;
}
# Print to Terminal
if($printToTerm==1){
print "@new\n";
}

These are the errors I'm receiving:

Global symbol "$dbh" requires explicit package name at ./zip.pl line 32.
Global symbol "$passwd" requires explicit package name at ./zip.pl line
32.
Global symbol "$dbh" requires explicit package name at ./zip.pl line 33.
Global symbol "$action" requires explicit package name at ./zip.pl line
34.
Execution of ./zip.pl aborted due to compilation errors.
[mike@viper mike]$ ./zip.pl
Global symbol "$dbh" requires explicit package name at ./zip.pl line 32.
Global symbol "$passwd" requires explicit package name at ./zip.pl line
32.
Global symbol "$dbh" requires explicit package name at ./zip.pl line 33.
syntax error at ./zip.pl line 33, near "$tablename("
Bareword found where operator expected at ./zip.pl line 33, near ")VALUES"
        (Missing operator before VALUES?)
Global symbol "$action" requires explicit package name at ./zip.pl line
34.
Execution of ./zip.pl aborted due to compilation errors.


I know that I can connect to the DB.   I can connect using this script:

#!/usr/bin/perl

use DBI;
$user='notshown';
$passwd='notshown';
 $dbh = DBI->connect('DBI:Sybase:server=192.168.0.2', $user, $passwd);
 $dbh->do("use Northwind");
 $action = $dbh->prepare("sp_help");
 $action->execute;
 $rows = $action->rows;
 print "rows is $rows\n";

 while(@first = $action->fetchrow_array){
        foreach $field (@first){
                print "$field\t";
        }
        print "\n";
}
exit(0);


Any light you can shed on this would be greatly appriciated.  Thank you

Mike




_______________________________________________________________
Composed with Pine 4.2.1

FACT: George Washington was actually the 8th President of the United
States, but the first under our current constitution.



-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About