Examples for the SAP::DBTech::dbm Module

To create a connection, enter the corresponding arguments from the following examples on the command line.

Use the following command to call the sample.pl Perl script and log on to the DEMODB database instance as DBM operator with user name OLEG and password MONDAY.

perl sample.pl OLEG MONDAY DEMODB

Establishing a Connection

# To reference Perl libraries:

# ------------------------------

use SAP::DBTech::dbm;

# To parse the 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 show a shortened login process:

session = sapdb.dbm.DBM (’’, $ARGV [2], $ARGV [0] . ’,’ . $ARGV [1])

Listing all Databases

You can use the Methode cmd to execute DBM commands. 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 DBM command that lists 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. one line for each of the following kernel variants:

        # fast und slow.

        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 ();

Handling Error Situations

If an error occurs within a cmd method, an exception is generated. You can intercept this exception with eval and query it with 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 ();