Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create multiple database connection in the single subroutine .How can i do it?.Here i had used the same connection twice with two different databases.I am not clear whether is this is the right way to do?Can anyone suggest me the right way to connect multi[le databases in the single sub routine?

What I have tried:

PERL
sub connect {	
		$host     = "devgam.spd.analog.com";
		$database = "r_bang_1m" ;
		$user     = "db_cad" ;
		$pw       = "Cad123";
		my $dsn = "DBI:mysql:host=$host";
		my $dbh = DBI->connect($dsn, $user, $pw) ||
		die "ERROR: can't connect to database server\n";
		return $dbh;
		$host     = "devgam.spd.analog.com";
		$database = "r_bang_current" ;
		$user     = "db_cad" ;
		$pw       = "Cad123";
		my $dsn = "DBI:mysql:host=$host";
		my $dbh = DBI->connect($dsn, $user, $pw) ||
		die "ERROR: can't connect to database server\n";
		return $dbh;}
Posted
Comments
Peter Leow 7-Feb-17 0:04am    
It will always returns the first $dbh. If you want to get the second one when the first one fails, use if else https://www.tutorialspoint.com/perl/perl_if_elsif_statement.htm.
If you really want multiple connections, use different routines, else how do you know which connection it refers to?
example file 7-Feb-17 4:23am    
I feel if -elsif will be not the proper for this query.Because we dont know how many databases will be here.
example file 7-Feb-17 1:03am    
Even i too think about it .But am blank of getting any ideas

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900