Reputation: 33425
I have a Perl script which needs to invoke IBM db2 to process some commands. The trouble is that the db2 connection is clobbered at each invocation
This doesn't work:
$> db2 connect to foo
$> perl -e 'print `db2 list tables for schema bar|some_filter`;'
nor does this:
$> perl -e 'print `db2 connect to foo`; print `db2 list tables for schema bar|some_filter`;'
nor does this:
$> perl -e 'print `db2 connect to foo && db2 list tables for schema bar|some_filter`;'
In each case the connection is lost by the time the second command is executed.
This works:
#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 list tables for schema bar`;
but this doesn't:
#!/usr/bin/perl
print `db2 connect to foo`;
print `db2 "select count * from bar"`;
A workaround would be to generate a bash script from Perl and execute that, but it would be nicer to do it directly. Is there a way?
Upvotes: 0
Views: 308
Reputation: 9255
Why not use DBI
instead of a shell script? Then you can maintain the connection and reuse the existing connection for as long as you want.
Upvotes: 2
Reputation: 754450
Each separate invocation of db2
has to connect anew before it can do anything with the database, and when the invocation ends, the connection is terminated. So your observed behaviour is to be expected.
If you're using Perl, seriously consider using DBI.
If that's not an option, then you need to consider running the db2
program from your Perl script as a sub-process with a pipe leading to it on which you write commands and a pipe leading back from it. You would need to write your commands to db2
and then be able to parse the returned data so you can tell when it has finished responding and deal with error messages and data. There must be modules for that (maybe core module IPC::Open3 or IPC::Open2).
Upvotes: 2