in reply to (Ovid) Re: problems wrapping DBI
in thread problems wrapping DBI
package db_dave; use strict; use DBI; use vars qw/ @ISA /; @ISA = qw/DBI/; sub new { my $class = shift; my ($db, $username, $password) = @_; if ($password eq "") { print STDOUT "## enter password for " . $username . "\@" . $db + . ": "; $password = <STDIN>; chomp $password; if ($password eq "") {die;} } my $dbh = DBI->connect("dbi:Oracle:" . $db, $username, $password, +{ RaiseError => 1 }) || die "Cant' connect: $DBI::errstr"; my $data = { _dbh => $dbh }; bless $data, $class; print STDERR "\n## connected \n"; return $data; } sub disconnect { my ($self, $q, $err) = @_; my $dbh = $self->{ _dbh }; $dbh->disconnect; print STDERR "\n## disconnected \n\n"; return; } sub runsql_string { my ($self, $q, $err) = @_; my $dbh = $self->{ _dbh }; my $sth = $dbh->prepare($q); print STDERR "\n## runsql_string: $q \n"; $sth->execute || die; my $returnstring; my @array; while (@array = $sth->fetchrow) { $returnstring = $array[0];} my $stringcount = @array; if ($stringcount > 1) { die; } $sth->finish; if ($::DEBUG) { print STDERR "[0] " . $returnstring . "\n"; } return $returnstring; } =begin testing use strict; use DBI; use lib "./"; use db_dave; $::DEBUG = "1"; # prints query results my $dbh = new db_dave("foo", "foo", "foo"); my $err = "error"; $q = "select count(*) from table"; $dbh->runsql_string($q, $err) . "\n"; $dbh->disconnect; =end testing
|
|---|