Gossamer Forum
Home : General : Perl Programming :

DBI Help!

Quote Reply
DBI Help!
Hi,

i've written a simple script to insert new records in a postgres table. But when i try to execute it, this doesn't work!

anyone can help-me?

script code:

#!/usr/bin/perl

use DBI;
use CGI;

my $query = new CGI;

print $query->header;

$DB = "/web/sites/30/buscaweb/www.buscaweb.f2s.com/db/index.db";
$NUM = "/web/sites/30/buscaweb/www.buscaweb.f2s.com/db/num.db";

my $data_source = 'dbi:Pg:dbname=buscaweb host=db.buscaweb.f2s.com';
my $username = 'buscaweb';
my $auth = 'mypassword';
my $dbh = DBI->connect($data_source, $username, $auth);
if ($dbh) {
open(LINKS, "$DB");
@links = <LINKS>;
close(LINKS);

foreach $line (@links) {
chomp($line);
($id, $title, $url, $description) = split(/\|/,$line);
$nid = &next();
$query = "INSERT INTO home (id, title, info, url) VALUES ($nid, $title, $description, $url)";
$ret = $dbh->do($query);
if($ret) {
print "ID: $nid - Added<br>\n";
}
}
$dbh->disconnect;
}
print $query->end_html;

sub next {
my $num;
open (NUM, "$NUM");
$num = <NUM>;
close (NUM);
$num++;
open (NUM, ">$NUM");
flock(NUM, 2);
print NUM $num;
flock(NUM, 8);
close (NUM);
return $num;
}

------------------
[]'s

Lucas Saud - #34750464
Quote Reply
Re: DBI Help! In reply to
1) Describe "Doesn't work" as we don't have esp.

2) why are you storing the sql statement in $query, when $query is your CGI object?

--mark
Quote Reply
Re: DBI Help! In reply to
Hi Mark,

i've fix all errors and now this script work!

new code:

#!/usr/bin/perl

use DBI;
use CGI;

my $CGI = new CGI;

print $CGI->header;

$DB = "/web/sites/30/buscaweb/www.buscaweb.f2s.com/db/index.db";
$NUM = "/web/sites/30/buscaweb/www.buscaweb.f2s.com/db/num.db";

my $Server = 'dbi:Pg:dbname=buscaweb host=db.buscaweb.f2s.com';
my $User = 'buscaweb';
my $Auth = 'mypassword';
my $DBH = DBI->connect($Server, $User, $Auth);
if ($DBH) {
open(LINKS, "$DB");
@links = <LINKS>;
close(LINKS);

foreach $line (@links) {
chomp($line);
($id, $title, $url, $description) = split(/\|/,$line);
$nid = &next();
$Query = qq!INSERT INTO home (id, title, info, url) VALUES ('$nid', '$title', '$description', '$url')!;*
$Ret = $DBH->do($Query);
if($Ret) {
print "ID: $nid - Added<br>\n";
}
}
$DBH->disconnect;
}
print $CGI->end_html;

sub next {
my $num;
open (NUM, "$NUM");
$num = <NUM>;
close (NUM);
$num++;
open (NUM, ">$NUM");
flock(NUM, 2);
print NUM $num;
flock(NUM, 8);
close (NUM);
return $num;
}

* Note: All strings should be enclosed between single quotes: 'string'.

[This message has been edited by Lucas (edited January 30, 2000).]