Procedure documentationExample: Executing DBM Commands Locate this document in the navigation structure

 

In this example, you use the cmd method (DBM class) from the sdb.sql module to execute the db_enum DBM command (display a list of all registered databases) and format the output.

Procedure

  1. Create a Python script sample.py with the following contents:

    Syntax Syntax

    1. # Import Python modules
      # ----------------------------
      import sys
      import sdb.dbm
      #
      # Open Database Manager session and logon to the database
      # ------------------------------------------------------
      session = sdb.dbm.DBM ()
      # Execute db_enum DBM command (List all registered databases)
      # Result: string
      output = session.cmd ('db_enum')
      dbstate = 'offline'
      lastdb = ''
      # Separate results using line brakes
      for line in output.split ('\n'):
        if not line:
          continue
        # Separate data fields using tabs
        database_name, installation_path, version, kernel_variant, operational_state = line.split ('\t')
        if database_name != lastdb:
        # Several lines are displayed for each database:
        # one for each kernel variant
          if lastdb != '':
            print lastdb, '\t', dbstate
          lastdb = database_name
          dbstate = 'offline'
        # The database is online if one of the kernel variants
        # is displayed as 'running'
        if operational_state == 'running':
          dbstate = operational_state
      print lastdb, '\t', dbstate
      
    End of the code.
  2. Call the Python script:

    python sample.py

    DEMODB running

    TESTDB offline