Procedure documentationExample: Exporting a Table Locate this document in the navigation structure

 

In this example, you use the cmd method (Loader class) from the sdb.loader module to start the Loader, log on to a database, and export a table.

Procedure

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

    Syntax Syntax

    1. # Import Python modules
      # -------------------------------------------
      import os
      import sys
      import sdb.loader
      # Determine home directory
      # -------------------------------------------
      homedir = None
      if sys.platform[:3] in ['win']:
        homedir= os.path.join(os.getenv ('HOMEDRIVE'), os.getenv ('HOMEPATH' ))
      else:
        homedir = os.getenv ('HOME')
      # Call Loader
      # -------------------------------------------
      session = sdb.loader.Loader ()
      #Logging On to the Database
      # -------------------------------------------
      session.cmd ("USE USER MONA RED DB DEMODB") 
      # Execute Loader command (export)
      # -------------------------------------------
      catalogstream = "CATALOG OUTSTREAM '%s' " % os.path.join(homedir, 'room_ddl.catalog')
      datastream = "DATA OUTSTREAM '%s' " % os.path.join(homedir, 'room_csv.data')
      cmd = "EXPORT TABLE HOTEL.ROOM %s %s " % (catalogstream,datastream)
      session.cmd (cmd)
      # Close connection to the database
      # -------------------------------------------
      session.release ()
      
    End of the code.
  2. Call the Python script:

    python sample.py

Result

The database catalog data and the application data in table HOTEL.ROOM have been exported to the output files room_ddl.catalog and room_csv.data in the home directory of the operating system user currently logged on.