Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Anonymous Monk
on Mar 30, 2017 at 05:29 UTC ( #1186445=note: print w/replies, xml ) Need Help??


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

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; }
  • Comment on Re: How to pass hash site variables into sql connection using perl?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 06:03 UTC
    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' }, );
Re^2: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 05:47 UTC
    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
        I have posted the code .Please look into it.
        I had to pass like this how can i pass it for the above script which you had included? $site{$site}{'current'} = "a_$site\_current";
        use Getopt::Long; 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 => 'am.d.aog.com', db=>'site1', user=>'db_cad', pw= +>'Cad123' }, site2 => { host => 'am.d.aog.com', db=>'site2', user=>'db_cad', pw +=>'Cad123' }, ); my $dbh = dbconnect($site); print $dbh; 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}{'current'}{user}, $site +_map{$s}{'current'}{pw}) or die "ERROR: can't connect to database server site $s"; return $dbh; }
        This is how i tried from your code.
        i got the following error
        usage: jan/bin/r.cgi site_name
        i had used current because every database name will have current.So i tried to include those things inside it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-02-29 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (128 votes). Check out past polls.

    Notices?