A Brief Explanation for Carwyn Edward's Python and PostgreSQL Examples

Note: most of the contents here are extracted from the comments in the Python code of the examples and the README file provided with the examples.

1. What are in the examples?

The examples contain five python programs:

The other files:

  • web/index.html: an HTML file containing links to all the four Python CGI examples
  • web/example.sql: the file used to create the tables needed by the "web/cgi/database.cgi" example
  • web/startup.py: a very simple web server written in Python needed by the CGI program
  • web/README: instructions on how to run the examples

2. How to run the examples?

Note: I installed the examples on my homepage. You could visit: here to get an idea what the examples do before you follow the steps below.(I will remove them from my homepage after the homework deadline.)

  • test if you can access the PostgreSQL database server(e.g. postgres.inf.ed.ac.uk):
psql -h postgres -U your_user_name (usually the same as your user name for DICE)
  • extract the files: they are listed above, or you can obtain them from the tarball with
tar -xzvf python-examples-0.10.0.tar.gz
  • create the database needed by using the command
psql -h postgres -U your_user_name -f web/example.sql
  • change the database connection information in "database/dbexample.py" and "web/cgi/database.cgi":
user = 'your user name'(the user name you used in the first step as psql -U parameters)
dbname = 'your database name' (you default database is the same as your user name)
passwd = 'your password'
Note: if you use other database servers you also need to change "host" and "port" variable
  • run "database/dbexample.py":
cd database
python
Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbexample
>>> dbexample.pgExample()
>>> dbexample.pgdbExample()
  • run the web server:
web/startup.py
Note: you could use other web server
  • open "http://127.0.0.1:8080/" in a web browser
You will see the content of the index.html file which contains links to the four CGI examples.
You can click the links to try the examples.

2. A tip for Python

You could use dir() and help() to get information for the current environment, for example:

cd database
python
>>> dir()
>>> import dbexample
>>> dir(dbexample)
>>> help(dbexample)
>>> help(dbexample.pgExample)

3. A tip for PostgreSQL

You could use "psql -lE" to list all available databases and show the internal generated SQL command.

4. More resources

  1. python-examples-0.10.0.tar.gz: Carwyn's Python and PostgreSQL examples.
  2. the file web/README provided with examples: web/README
  3. the comments in the Python programs in the examples
  4. A Python Tutorial
  5. PostgreSQL 7.3.13 Documentation(the version on postgres.inf.ed.ac.uk is 7.3)
  6. A Brief Guide for Using Galax on DICE
Topic revision: r3 - 08 Feb 2006 - 19:26:10 - XibeiJia
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
This Wiki uses Cookies