awohld has asked for the wisdom of the Perl Monks concerning the following question:
Main Codepackage My::DB; use strict; use DBI; # Postgres Username/Password $My::DB::pg_username = 'my_username'; $My::DB::pg_password = 'my_password'; sub new { my $data = $_[1]; my $dbh; if ( $data->{server} eq 'pg' ) { $dbh = DBI->connect("dbi:Pg:dbname=$data->{db};user=$My::DB::p +g_username;password=$My::DB::pg_password") or die "Unable to connect: $DBI::errstr\n"; $dbh->{PrintError} = 1; $dbh->{RaiseError} = 1; } if ( $data->{server} eq 'sqlite' ) { $dbh = DBI->connect("dbi:SQLite:dbname=$data->{db}","","",{Pri +ntError => 1,AutoCommit => 1}); } return $dbh; }
Edit: Updated using plobsing's suggestion.#!/usr/bin/perl -w use strict; use My::DB; # connect SQLITE my $dbh = My::DB->new({server => 'sqlite', db => 'test.db'}); # connect PostgreSQL #my $dbh = My::DB->new({server => 'pg', db => 'test'}); my $sth_select = $dbh->prepare("SELECT * FROM tbl1"); $sth_select->execute; while ( my @row = $sth_select->fetchrow ) { print "@row \n"; }
|
---|