Mark Knoop wrote:
> Hello
>
> I am trying to rationalize some of my code and would like to use a set of
> common functions which do database stuff using DBI.
>
> The most logical way for me to do this would be to create a 'GetDBHandle'
> function which passes back a handle to the database connection which can
> then be passed to other functions which involve individual common queries.
>
> Bearing in mind that:
>
> a) I am unfamiliar with OO principals in perl
> b) I am a bit rusty on perl modules in general
> c) as well as wanting to do this properly I would like to keep it as simple
> as required
>
> could anyone give me pointers for things to bear in mind.
>
> I'm looking at something like the following:
>
> ###############################################################
>
> use strict;
> use warnings;
> use DBI;
> use DBD::ODBC; # can this live solely in the module?
> use Common::CSAdmin; # this is my module
>
> my $dbh = GetDBHandle('DSN');
See below.
> my $sql = <<EOF;
>
> SELECT Test FROM tblTest
>
> EOF
>
> my $sth = $dbh->prepare($sql);
>
> $sth->execute() or die "$!";
>
> while (my ($test) = $sth->fetchrow) {
> print $test . "\n";
> }
>
> $sth->finish;
>
> ################################################################
>
> which calls
>
> #################################################################
>
> package Common::CSAdmin;
>
> use strict;
> use warnings;
>
> use DBI;
> use DBD::ODBC;
You could set up an export list here or qualify your call in main above :
my $dbh = Common::CSAdmin::GetDBHandle('DSN');
> sub GetDBHandle($) {}
Remove the above line or if you want a prototype somewhere replace
the ' {}' with ';' (Don't know why you would want a prototype one
line ahead of the actual sub though.)
> sub GetDBHandle($) {
> my $dsn = shift(@_);
> my ($dbusr,$dbpwd,$server,$dbh);
> $dbusr = 'usr';
> $dbpwd = 'pwd';
> $server = 'server';
> $dbh = DBI-> connect("DBI:ODBC:$dsn", $dbusr, $dbpwd, {RaiseError => 1,
> PrintError => 1}) || die "Could not connect to datasource $dsn";
> return $dbh;
> }
>
> 1;
>
> #####################################################################
>
> but to start with I am getting the following
>
> Subroutine GetDBHandle redefined at D:\Code\iis/Common/CSAdmin.pm line 11.
> Undefined subroutine &main::GetDBHandle called at testcsadmin.pl line 7.
>
> which I'm not sure about. Aside from this I guess I have to do something a
> bit more clever with the returning of $dbh but I am having trouble finding
> examples. Any thoughts?
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs