Python Code Samples

This section lists the Python functions resident in the example.py module, which you can see at the Section called Code: example.py.

Running the Sample Module (example.py)

  1. If they are not already installed, get and install the rh-postgresql-python package. You can find these on your Red Hat Database CD; updates are available from Red Hat Network.

  2. To accept TCP/IP connections, edit the configuration file to allow TCP/IP connections. For a standard installation of Red Hat Database, the configuration file is located at /var/lib/pgsql/data/postgresql.conf. Modify and uncomment the value of the tcpip_socket from false to true so that TCP/IP connections are allowed by default every time the postmaster starts up. Alternatively, you could (re)start the postmaster with the -i option.

  3. Start the postmaster.

  4. Create a database called basketball using your userid. If your userid is not allowed to create databases, talk to your database administrator or refer to the Red Hat Database Administrator and User's Guide for instructions on how to grant "create database" permission to a user.

  5. Create the sample python module, example.py. (See the Section called Code: example.py).

  6. Make sure example.py is accessible to the Python interpreter. You may need to update the PYTHONPATH environment variable on your system to include the directory in which example.py resides.

  7. Invoke the Python interpreter on the command line:

    $ python

    This creates an interactive session for you. You should see a ">>>" prompt.

  8. Import the example module to allow access to the defined functions. The Red Hat Database Python DB-API module, pgdb.py, is imported by the example.py module:

    >>> import example
  9. Display information about the RHDB Python Database API. A function, about(), has been provided to display this information:

    >>> example.about ()

    Displays:

    ******************************************
     About the Red Hat Database Python DB-API
       DB-API level: 2.0
       DB-API Thread Safety level: 1
       DB-API Parameter Formatting: pyformat
    ******************************************
  10. Create a table called players within the basketball database and seed the table with information as listed under the Section called Database Schema. A function, cleantable() will drop the players table (if it exists), create the table, seed it, and display the table data.

    >>> example.cleantable ()

    Displays:

    Dropping Players table...
       Exception encountered: Table does not exist. Continuing...
       Creating and seeding Players table...
          Players table successfully created.
    	 
    Content of Players table:
    *************************
     Michael Jordan -- Washington Wizards
     Tim Duncan -- San Antonio Spurs
     Vince Carter -- Toronto Raptors
    
    Number of players: 3
  11. The transaction() function is an example that contains a transaction with the following SQL statements:

    insert into players values ('Tracy McGrady','Orlando Magic')
    insert into players values ('Kobe Bryant','NY Knicks')
    delete from players where name = 'Michael Jordan'

    To execute the SQL statements in a transaction and rollback at the end of the transaction:

    >>> example.transaction ("rollback")

    Displays:

    Before:
    Content of Players table:
    *************************
     Michael Jordan -- Washington Wizards
     Tim Duncan -- San Antonio Spurs
     Vince Carter -- Toronto Raptors
    
    Number of players: 3
    
    About to issue the following commands: 
      insert into players values ('Tracy McGrady','Orlando Magic')
      insert into players values ('Kobe Bryant','NY Knicks')
      delete from players where name = 'Michael Jordan'
    
    About to rollback the transaction...
    
    After:
    Content of Players table:
    *************************
     Michael Jordan -- Washington Wizards
     Tim Duncan -- San Antonio Spurs
     Vince Carter -- Toronto Raptors
    
    Number of players: 3

    To execute the SQL statements in a transaction and commit at the end of the transaction:

    >>> example.transaction ("commit")

    Displays:

    Before:
    Content of Players table:
    *************************
     Michael Jordan -- Washington Wizards
     Tim Duncan -- San Antonio Spurs
     Vince Carter -- Toronto Raptors
    
    Number of players: 3
    
    About to issue the following commands: 
      insert into players values ('Tracy McGrady','Orlando Magic')
      insert into players values ('Kobe Bryant','NY Knicks')
      delete from players where name = 'Michael Jordan'
    
    About to commit the transaction...
    
    After:
    Content of Players table:
    *************************
     Kobe Bryant -- NY Knicks
     Tim Duncan -- San Antonio Spurs
     Tracy McGrady -- Orlando Magic
     Vince Carter -- Toronto Raptors
    
    Number of players: 4
  12. You may notice that Kobe Bryant's team is listed incorrectly. Perform an update on the players table to change Kobe's team to the LA Lakers. In the example function, update(), the current data for Kobe is returned, an SQL update is performed and Kobe's data is requeried. The query result sets are not formatted in this example.

    >>> example.update ()

    Displays:

    Before: [['Kobe Bryant', 'NY Knicks']]
    
    Updating...
    
    After: [['Kobe Bryant', 'LA Lakers']]
    
    Content of Players table:
    *************************
     Kobe Bryant -- LA Lakers
     Tim Duncan -- San Antonio Spurs
     Tracy McGrady -- Orlando Magic
     Vince Carter -- Toronto Raptors
    
    Number of players: 4
  13. Press Ctrl-D to exit from the Python interpreter.