David Parker dparker
Tue Jul 27 15:13:48 PDT 2004
Thanks!

- DAP 

-----Original Message-----
From: Daniel P. Berrange [mailto:dan at berrange.com] 
Sent: Tuesday, July 27, 2004 11:10 AM
To: David Parker
Cc: slony1-general at gborg.postgresql.org
Subject: Re: [Slony1-general] perl guidance

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
-=|



More information about the Slony1-general mailing list