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

 

In this example, you use the cmd method (DBM class) and the DBMServError class from the sdb.dbm module. You execute an incorrect DBM command and handle the error.

Procedure

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

    Syntax Syntax

    1. # Import Python modules
      # ----------------------------
      import sys
      import sdb.dbm
      #
      # Parse call arguments
      # --------------------------
      dbm_operator = sys.argv [1]
      dbm_operator_password = sys.argv [2]
      database_name = sys.argv [3]
      #
      # Call the Database Manager and log on to a database
      # ------------------------------------------------------
      session = sdb.dbm.DBM ('', database_name, '', dbm_operator + ',' + dbm_operator_password)
      # Execute incorrect DBM command
      for cmd in ['db_state', 'invalid_command']:
        try:
          result = session.cmd (cmd)
      # Display result
          print cmd + ': OK ', repr (result)
        except sdb.dbm.DBMServError, err:
      # Display error message
          print cmd + ': ERR', err.errorCode, err.message
      
    End of the code.
  2. Call the Python script:

    python sample.py DBADMIN SECRET DEMODB

    db_state: OK 'State\nONLINE\n'

    my_invalid_command: ERR -24977 Unknown DBM command "inval