To create a connection, copy the corresponding arguments from the following examples to the command line.
Use the following command to call the sample.py Python script and log on to the HOTELDB database instance with the DBM user name and password.
python sample.py DBM DBM HOTELDB
# To reference Perl libraries:
# ------------------------------
use SAP::DBTech::dbm;
# To parse call arguments:
# --------------------------
my $user_name = $ARGV [0];
my $password = $ARGV [1];
my $database_name = $ARGV [2];
# To create a Database Manager session:
# ---------------------------------------
my $session = new DBM ('', $database_name, '', "$user_name,$password");
# To log off:
# -------------------------------------
$session->release ();
The following examples specify a shortened login process:
session = sapdb.dbm.DBM (’’, $ARGV [2], $ARGV [0] . ’,’ . $ARGV [1])
You can use the cmd method to execute Database Manager statements. The result is a character string that can be further processed using Perl.
# To reference Perl libraries:
# ------------------------------
use SAP::DBTech::dbm;
# To create a Database Manager session:
# ------------------------------------------------------
my $session = new DBM ('', $ARGV [2], '', $ARGV [0] . ',' . $ARGV [1]);
# To execute the command for listing all database
# instances. (The result is a character string.):
output = session.cmd ('db_enum')
my $dbstate = 'offline';
my $lastdb = '';
# Individual database instances are separated by line breaks.
foreach my $line (split /\n/, $output){
# Data fields are separated by tab characters.
my ($name, $instroot, $release, $kind, $state) = split /\t/, $line;
if ($name ne $lastdb) {
# Several lines exist for each database instance,
# max. of one line for each of the following core variants:
# fast, quick, slow and test.
if ($lastdb ne '') {
print "$lastdb\t$dbstate\n";
}
$lastdb = $name;
$dbstate = 'offline';
}
# The database is active if one of the core variants
# is displayed as 'running'.
if ($state eq 'running') {
$dbstate = $state;
}
}
print "$lastdb\t$dbstate\n";
$session->release ();
If an error occurs within a cmd method, an exception is generated. This exception can be intercepted by eval and queried using the $@ system variable.
# To reference Perl libraries:
# ------------------------------
use SAP::DBTech::dbm;
# To create a Database Manager session:
# -----------------------------------------
my $session = new DBM ('', $ARGV [2], '', $ARGV [0] . ',' . $ARGV [1]);
foreach my $cmd (('db_state', 'invalid command')) {
eval {
my $result = $session->cmd ($cmd);
# To output the result:
print "$cmd: OK $result\n";
};
if ($@) {
# To output the error message:
print "$CMD: ERR $@\n";
}
}
$session->release ();