Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to pass hash site variables into sql connection using perl?

by finddata (Sexton)
on Mar 30, 2017 at 05:07 UTC ( #1186444=perlquestion: print w/replies, xml ) Need Help??

finddata has asked for the wisdom of the Perl Monks concerning the following question:

I like to connect multiple hash variables with mysql database connection?How can i do it.Suppose if have four database names in hash i want to pass those database names as variables for database connection .How can i do it?
use strict; use warnings; use Getopt::Long; my $site; GetOptions("site=s" => \$site) or die "Error in command line arguments\n"; if($site eq ''){ $site = 'site1'; } defined $site or die "usage: $0 site_name\n"; my %site_map = ( site1 => [ qw(a_site1_current) ], site2 => [ qw(a_site2_current) ], site3 => [ qw(a_site3_current) ], ); sub connect { $host = "dam.sd.aog.com"; $database = "a_$site_map\_current" ; $user = "ad" ; $pw = "123"; my $dsn = "DBI:mysql:host=$host"; my $dbh = DBI->connect($dsn, $user, $pw) || die "ERROR: can't connect to database server\n"; return $dbh; } sub ShowContents { my @arr = @{$site_map{$site}}; #print @arr; }
I had tried the above manner to pass all hash table site names into the database connection but my code is not working.Help me to fix the issue.Thanks for any help in advance.

Replies are listed 'Best First'.
Re: How to pass hash site variables into sql connection using perl?
by Anonymous Monk on Mar 30, 2017 at 05:29 UTC
    use strict; use warnings; use Getopt::Long; use DBI; GetOptions("site=s" => \my $site) or die "Error in command line arguments\n"; defined $site or die "usage: $0 site_name\n"; my %site_map = ( site1 => { host => 'host1', db=>'db1', user=>'user1', pw=>'pw1' }, site2 => { host => 'host2', db=>'db2', user=>'user2', pw=>'pw2' }, site3 => { host => 'host3', db=>'db3', user=>'user3', pw=>'pw3' }, ); my $dbh = dbconnect($site); sub dbconnect { my $s = shift; die "bad site $s" unless $site_map{$s}; my $dsn = "DBI:mysql:database=$site_map{$s}{db};host=$site_map{$s} +{host}"; my $dbh = DBI->connect($dsn, $site_map{$s}{user}, $site_map{$s}{pw +}) or die "ERROR: can't connect to database server site $s"; return $dbh; }
      I have few doubts: 1.Why you can given different database names like (db1,db2,db3).My database name should be a_$site1_current.Only the $site1 should be changed as per hash.

      2,In the above code i had not fount any words which matches as like a_site1_current.

        I have few doubts:

        what you have is little concern about doing your own work

        1.Why you can given different database names like (db1,db2,db3).

        because it is an generic example, that someone with any ability at all to think for themselves should be able to modify by themselves to suit their needs

        My database name should be a_$site1_current.Only the $site1 should be changed as per hash.

        A normal person should be able to fix the example themselves

        2,In the above code i had not fount any words which matches as like a_site1_current.

        Well what could you change to fix that? Can you not even think for yourself that much? Or do we need to write all your code for you? Are you going to pay us to do that? You need to tell your boss that you are not skilled enough to do your own work.

        site1 => { host => 'dam.sd.aog.com', db =>'a_site1_current', user =>'ad', pw= >'123' },

        ps: i dont see 36 lines in any here script so far, so of course you havent shown us what you have actually tried, and if this is actually a cgi program you wont have a lot of luck with GetOptions, you will need something like use CGI; my $site=$q->param('site'); instead.

        It was an example. You need to fill in your details like this

        my %site_map = ( site1 => { host => 'dam.sd.aog.com', db => 'a_site1_current', user=>'ad', pw=>'123' }, site2 => { host => 'dam.sd.aog.com', db => 'a_site2_current', user=>'ad', pw=>'123' }, site3 => { host => 'dam.sd.aog.com', db => 'a_site3_current', user=>'ad', pw=>'123' }, );
      Not enough arguments for connect at /jan/bin/r.cgi line 36, near "$site)"

        Please post the code that gives that error.
        I guess you need to rename your sub to dbconnect like shown here. You can't use connect because it is a DBI method.

        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1186444]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2019-12-06 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (154 votes). Check out past polls.

    Notices?