For Programmers: Free Programming Magazines  


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
Alma

2007-06-22, 9:59 pm

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.




Chas Owens

2007-06-22, 9:59 pm

On 6/22/07, Alma <almatirkey@gmail.com> wrote:
> Hi All,
>
> I wanted to make a database connection using the values provided
> in .cfg file

snip

Don't roll your own config parser. There are many in CPAN. I would
suggest using YAML* or YAML::Syck*. The latter is a faster
implementation, but it requires the syck library.


* http://search.cpan.org/~ingy/YAML-0.65/lib/YAML.pm
* http://search.cpan.org/~audreyt/YAM...b/YAML/Syck.pod
Vista

2007-06-25, 5:38 pm

Catherine Z. Jones and Paris Hilton Have Fun!
http://www.eyrenet.com/watch?clip=1673286

Ashlee Simpson and Laetitia Casta In A Lesbian Catfight!
http://www.eyrenet.com/WindowsMediaPlayer?q=1673286

Tara Reid and Tara Reid Vibrating In Slits!
http://www.eyrenet.com/Play?id=1673286

Catherine Z. Jones and Olsen Twins Licking Pussy!
http://www.eyrenet.com/c?vid=1673286

Carmen Electra and Mariah Carey , Lesbian Blondes On Table!
http://www.eyrenet.com/Play?q=1673286

xxx free movie seventeen hardcore movie capture streaming video clip dump porn free porn star movie
free music video watch crazy video clip 30 ipod video zoofilia foto gratis girl music spice video
scary movie 2 download midget sex movie free online black porn video vintage sex movie britney spears crotch shot
hardcore movie porn xxx [url=http://new-dam-hussein-execution.org/hussein-iraq-legacy-dam.html]hussein iraq legacy dam[/url] armband ipod video paris hilton girl gone wild movie trailer yahoo

free wild sex video
aniversario de gratis postales
free paris sex video
listen to usher dot com
dale dvd movie
gratis musica video y yahoo
video game trailer
sex movie teen
video xxx gratis
funny bush video
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com