CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Db2 database connection

 



ilugopal
Novice

Sep 22, 2016, 2:29 PM

Post #1 of 7 (3072 views)
Db2 database connection Can't Post

Hi all - I connecting to a remote database using db2.
Here is my code. I got the user id and password from parsing a file.
my $filename = 'connect_01.sql';
if (open(my $fh, $filename)) {
while (my $row = <$fh>) {
chomp $row;
my ( $database, $username, $password ) = split /:/,$row;
print "$row\n";
print "$database\n";
print "$username\n";
print "$password\n";
chdir ("$Bin_Dir") or die "fail $!";
system ('db2cmd.exe' ,'db2 connect to $database user $username using $password' );
}

The $database , $username and $password is not working however if I replace with actual value it is working perfectly fine and able to connect to the database.

can anyone help what mistake I'm doing here?

Alan...


FishMonger
Veteran / Moderator

Sep 22, 2016, 2:33 PM

Post #2 of 7 (3071 views)
Re: [ilugopal] Db2 database connection [In reply to] Can't Post

Variables won't interpolate inside single quoted strings. Change them to double quotes.


ilugopal
Novice

Sep 22, 2016, 3:21 PM

Post #3 of 7 (3068 views)
Re: [FishMonger] Db2 database connection [In reply to] Can't Post

Thank you and its working. I'm good at shell and not in PERL. Need more help.

chdir ("$Bin_Dir") or die "fail $!";
#system ('db2cmd.exe' ,"db2 connect to $database user $username using $password");
system ('db2cmd.exe' ,"db2 -tvf ${Properties_Dir}connect.sql" );
system ('db2cmd.exe' ,"db2 -tvf ${OneTime_Dir}test.sql" );

I connect to the database using the second system command given above. But the session is not lasting for next system command for to execute test.sql complains that there is not database connection. I may have more .sql inside OneTime_Dir and need to execute in a particular order.
Any guess please ?


FishMonger
Veteran / Moderator

Sep 22, 2016, 5:01 PM

Post #4 of 7 (3058 views)
Re: [ilugopal] Db2 database connection [In reply to] Can't Post

The system function forks off a child process to run your command and returns the exit code of that command. It does not keep an open connection to your database.

When interacting with a database, you should be using Perl's DBI module which is a database independent interface for Perl. In addition to that, you also will need a db driver module. In this case that would the DBD::DB2 module.

http://search.cpan.org/~timb/DBI-1.636/DBI.pm
http://search.cpan.org/~ibmtordb2/DBD-DB2-1.85/DB2.pod


ilugopal
Novice

Sep 22, 2016, 7:24 PM

Post #5 of 7 (3051 views)
Re: [FishMonger] Db2 database connection [In reply to] Can't Post

Yeah I got that DBI directly from Perl Manager however to install the DBD::DB2 it need a nmake for that I need to install Visual Studio. Getting into a tough loop to accomplish a simple script.
Thanks for the reply. Will let you all know the progress...


FishMonger
Veteran / Moderator

Sep 23, 2016, 6:16 AM

Post #6 of 7 (3045 views)
Re: [ilugopal] Db2 database connection [In reply to] Can't Post

You do not need Visual Studio.

There are several ways to get the compiler without having to buy Visual Studio. One way would be to install the MinGW module which provides a compiler.

The best and easiest solution would be to upgrade perl by installing Strawberry perl which provides a gnu compiler.
http://strawberryperl.com/

Staying with your current 5.8.6 severely limits you not only in the ability to install modules but you also can't take advantage of all of the improvements in perl since 2004.


ilugopal
Novice

Sep 23, 2016, 6:54 AM

Post #7 of 7 (3042 views)
Re: [FishMonger] Db2 database connection [In reply to] Can't Post

FishMonger - I truly appreciate your dedication to the forum. Truly said and yesterday the dmake of perl failed. I'm going to install starwberry perl and will keep you updated.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives