Home > Archive > PERL Beginners > June 2007 > Database connection using cfg file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Database connection using cfg file
|
|
|
| Hi All,
I wanted to make a database connection using the values provided
in .cfg file
my.cfg
***********
dbname=xyz
user=abc
passwd=abc123
***********
my perl package which need to read the my.cfg file & make the
connection
mypackage
************
sub new(){
my @arr=();
readcfg();
#foreach (@arr) {
#print "$_ \n";
#print "for loop";
#}
$dbname=@arr[0];
$usr = @arr[1];
$pwd ||= @arr[2];
# ($self) = {};
# bless($self);
$databasehandle = DBI->connect("DBI:Pg:dbname=$dbname",$usr,
$pwd,{PrintError =>1});
if (!$databasehandle){
print "Database connection is not estabilished";
exit;
}
sub readcfg {
# $cfgpath = shift;
open(FH,"<my.cfg")or die "Can't open file";
while (my $line=<FH> ) {
chomp $line;
next if ($line =~ /^\s*#/);
next if ($line =~ /^\s*$/);
$line =~ s/#.*$//;
}
($Key,$Val) = ($line =~ /^\s*(\S+)\s*=\s*(.*\S)\s*$/);
push @arr,$Val;
return @arr;
}
return($self);
}
---------
I am getting an error
(DBI:connect ' ' ) failed .Fatal database 'root' doesn't exist.
Can any one suggest me how can we read the values from conf file & get
the database connection .
& i need to call the database handle to call the subroutine.
Thanks in advance.
| |
| Andrew Curry 2007-06-22, 9:59 pm |
| One thing could be
$dbname=@arr[0];
$usr = @arr[1];
$pwd ||= @arr[2];
They should be $arr[n..]
If you turn on strict you can get rid of these as it wont compile until you
do.
If you take your readcfg.
readcfg($cfgpath,\%cfgvalues);
sub readcfg {
my($cfgpath,$cfgvalues) = @_;
my($line,$key,$value);
open(FH,"<my.cfg")or die "Can't open file $!";
while ($line=<FH> ) {
chomp $line;
next if ($line =~ /^\s*#/);
next if ($line =~ /^\s*$/);
($key,$value)=split('=',$line);
$cfgpatch->{$key}=$value;
}
}
You can then access your values by $cfgpath{'database'} etc...
$dbname=$cfgpath{'database'};
Etc...
None of this has been run by the way so bound to be syntax errors.
-----Original Message-----
From: Alma [mailto:almatirkey@gmail.com]
Sent: 22 June 2007 11:28
To: beginners@perl.org
Subject: Database connection using cfg file
Hi All,
I wanted to make a database connection using the values provided in .cfg
file
my.cfg
***********
dbname=xyz
user=abc
passwd=abc123
***********
my perl package which need to read the my.cfg file & make the connection
mypackage
************
sub new(){
my @arr=();
readcfg();
#foreach (@arr) {
#print "$_ \n";
#print "for loop";
#}
$dbname=@arr[0];
$usr = @arr[1];
$pwd ||= @arr[2];
# ($self) = {};
# bless($self);
$databasehandle = DBI->connect("DBI:Pg:dbname=$dbname",$usr,
$pwd,{PrintError =>1});
if (!$databasehandle){
print "Database connection is not estabilished";
exit;
}
sub readcfg {
# $cfgpath = shift;
open(FH,"<my.cfg")or die "Can't open file";
while (my $line=<FH> ) {
chomp $line;
next if ($line =~ /^\s*#/);
next if ($line =~ /^\s*$/);
$line =~ s/#.*$//;
}
($Key,$Val) = ($line =~ /^\s*(\S+)\s*=\s*(.*\S)\s*$/);
push @arr,$Val;
return @arr;
}
return($self);
}
---------
I am getting an error
(DBI:connect ' ' ) failed .Fatal database 'root' doesn't exist.
Can any one suggest me how can we read the values from conf file & get the
database connection .
& i need to call the database handle to call the subroutine.
Thanks in advance.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org http://learn.perl.org/
This e-mail is from the PA Group. For more information, see
www.thepagroup.com.
This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.
Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.
| |
|
|
|
|
|
|
|