Daniel P. Berrange dan
Tue Jul 27 15:10:54 PDT 2004
On Tue, Jul 27, 2004 at 10:55:00AM -0400, David Parker wrote:
> But I haven't used DBI before, and I'm having difficulty discovering how
> to hook it in to postgres. One posting in the archives (which are
> incredibly slow today, it seems) mentioned DBD:PG, but I can't seem to
> find that on cpan.org.

You're just missing a semicolon there - DBD::Pg

  http://search.cpan.org/~rudy/DBD-Pg-1.32/
  
> I realize this is not really a slony question, but any guidance getting
> me started would be appreciated. Which API should I use? Where can I
> find it?

DBI is the generic database layer, DBD::Pg is the postgresql driver.
The man pages for both those modules give quite detailed API docs.
I believe there is an O'Reilly bok about DBI, but for basic intro
I'd just find a few short pieces of code to look through, since its
not that complicated an API.

As a starter the general pattern  I tend to use is something like:

  use DBI;
  
  my $db = DBI->connect("DBI:Pg:dbname=mydb;host=myhost", 
                        $username, $password, {
			  RaiseError => 1,
			  PrintError => 0,
			  AutoCommit => 0});
  eval {
    my $sth1 = $db->prepare("INSERT INTO foo (bar) values (?)");
    $sth1->execute($bar);
    
    my $sth2 = $db->prepare("SELECT bar, wizz FROM foo where bar > ?");
    $sth2->execute(20);
    
    my ($bar, $wizz);
    $sth2->bind_columns(\$bar, \$wizz);
    while ($sth2->fetchrow) {
        print "Got $bar $wizz\n";
    }

  };
  if ($@) {
    if ($db) {
      $db->rollback;
    }
    die $@;
  } else {
    $db->commit;
  }


Dan.
-- 
|=-            GPG key: http://www.berrange.com/~dan/gpgkey.txt       -=|
|=-       Perl modules: http://search.cpan.org/~danberr/              -=|
|=-           Projects: http://freshmeat.net/~danielpb/               -=|
|=-   berrange at redhat.com  -  Daniel Berrange  -  dan at berrange.com    -=|
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://gborg.postgresql.org/pipermail/slony1-general/attachments/20040727/f842d309/attachment.bin


More information about the Slony1-general mailing list