Procedure documentationExample: Querying Whether a Table Exists Locate this document in the navigation structure

 

In this example, you use the cmd method (Loader class) and the sql method (Loader class) of the sdb.loader module fto log on to a database and query whether a specific table exists.

Procedure

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

    Syntax Syntax

    1. # Import Python modules
      # -------------------------------------------
      import sys
      import sdb.loader
      # Call Loader
      # -------------------------------------------
      session = sdb.loader.Loader ()
      #Logging On to the Database
      # -------------------------------------------
      session.cmd ("USE USER MONA RED DB DEMODB")
      # Query whether table HOTEL.CUSTOMER exists.
      # If the table does not exist, the Loader returns
      # an error.
      # -------------------------------------
      sqlrc = session.sql("EXISTS TABLE HOTEL.CUSTOMER")
      if (sqlrc == 0):
        print 'Table HOTEL.CUSTOMER exists.'
      elif (sqlrc == -4004):
        print 'Error -4004: Table HOTEL.CUSTOMER does not exist.'
      # Close connection to the database
      # -------------------------------------
      session.release ()
      
    End of the code.
  2. Call the Python script:

    python sample.py