|
|
 | | From: | sudheer raghav | | Subject: | hoe to connect postgres database thru perl | | Date: | Tue, 18 Jan 2005 22:34:32 -0800 (PST) |
|
|
 | hi, am new to Perl. How to connect postgresql database with perl ? here is the Perl code follows: #!/usr/bin/perl use CGI qw(:standard); use DBI; use strict;
print "Content-type: text/html\n\n"; use CGI::Carp qw/fatalsToBrowser/; $query = new CGI; print $query->header;
$dbh = DBI->connect("dbi:Pg:dbname=test", "postgres", "", {AutoCommit => 1, Rais eError => 1});
if($query->param("submit") eq "save") { $sth = $dbh->prepare("SELECT ip_add from firewall where $source_add ='10.0.0.10'"); } else { print "error"; }
if(!defined($sth)) { print "ERROR: Unable to execute database query: $DBI::errstr\n"; exit; } $sth->execute;
$sth->finish;
print $query->start_html("FIREWALL");
print <<"EOF"; print "FIREWALL\n"; print "IP Adress:"; print $query->popup_menu(-name=>'IP Adress', -Values=>['10.0.0.1','10.0.0.2','10.0.0.3','10.0.0.4',10.0.0.5,10.0.0.6], );
print "Source IP Adress:"; print $query->textfield('numeric'); print "Destination IP Adress:"; print $query->textfield('Dest'); print $query->radio_group(-name=>'Disable Enable', -Values=>['Disable','Enable'], );
print "$query->submit('Action','save');
print $query->submit('Action','cancel')"; EOF print $query->end_html; $dbh->disconnect; exit(0);
===== Y.SUDHEER RAGHAV+919440521140
__________________________________ Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less. http://info.mail.yahoo.com/mail_250
---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
|
|
 | | From: | Sean Davis | | Subject: | Re: hoe to connect postgres database thru perl | | Date: | Fri, 21 Jan 2005 06:08:46 -0500 |
|
|
 | What error did you receive?
On Jan 19, 2005, at 1:34 AM, sudheer raghav wrote:
> hi, > am new to Perl. > How to connect postgresql database with perl ? here > is the Perl code follows: > #!/usr/bin/perl > use CGI qw(:standard); > use DBI; > use strict; > > print "Content-type: text/html\n\n"; > use CGI::Carp qw/fatalsToBrowser/; > $query = new CGI; > print $query->header; > > $dbh = DBI->connect("dbi:Pg:dbname=test", "postgres", > "", {AutoCommit => 1, Rais > eError => 1});
It is a good idea to do:
> $dbh = DBI->connect("dbi:Pg:dbname=test", "postgres", > "", {AutoCommit => 1, Rais > eError => 1}) || die "Unable to connect to database $DBI::errstr";
(i.e., add the die, just in case your server isn't started or some such thing....)
> if($query->param("submit") eq "save") { > $sth = $dbh->prepare("SELECT ip_add from firewall > where $source_add ='10.0.0.10'");
What is $source_add? I don't see it elsewhere in the code, so this code can't work. In fact, does it even compile? My guess is that it doesn't, as you have used strict (good) and $source_add is not declared. See my comments about setting up a simple script without using the CGI.
> } else { > print "error"; > } > > if(!defined($sth)) { > print "ERROR: Unable to execute database query: > $DBI::errstr\n"; > exit; > } > $sth->execute; > > > $sth->finish; > > print $query->start_html("FIREWALL"); > > print <<"EOF"; > > > print "FIREWALL\n"; > print "IP Adress:"; > print $query->popup_menu(-name=>'IP Adress', > > -Values=>['10.0.0.1','10.0.0.2','10.0.0.3','10.0.0.4',10.0.0.5,10.0.0.6 > ],
Although I imagine you have a specific project you want to accomplish, it is usually a good idea to test new modules, etc. outside of the CGI environment. Write a toy script that connects to the database, does a query, prints the result, and then closes the database connection. Then, when there are problems, it will be clearer what is going on.
Also, while the DBI documentation is enormous, it is packed with useful information and examples. If you are going to be using DBI much, read the manual thoroughly and use the examples.
HTH, Sean
---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
|
|
|