DBMaker Starting Guides

The Starting Guide for Python


1. Background

We suppose that you have the basic concept about Python. For more information about Python, please reference the Python home site.

2. Installation

  1. Download the ODBC Driver for Python
    MxODBC is a ODBC driver for Python. Because it is not freeware, you should download and build it by yourself. You should download both mxDateTime and mxODBC. You can find information about mxODBC from here.

  2. Install the ODBC driver
    Please follow these steps to install mxODBC driver:

    1. Unpack the mxDateTime package.
      $ cd PYTHON_HOME/lib/python1.5/site-packages
      $ unzip mxDateTime-1.3.0.zip
      
      Where PYTHON_HOME is the base directory where the Python is installed. e.g. /usr/local/lib/python1.5/site-packages/ on Unix. This create a directory 'DateTime' in it.

      If there is not a site-packages directory, please create one.

    2. Build and install the module.
      $ cd DateTime/mxDateTime
      $ make -f Makefile.pre.in boot
      $ make
      
    3. Unpack the mxODBC package.
      $ cd PYTHON_HOME/lib/python1.5/site-packages
      $ unzip mxODBC-1.1.1.zip
      
      Where PYTHON_HOME is the base directory where the Python is installed. e.g. /usr/local/lib/python1.5/site-packages/ on Unix. This create a directory 'ODBC' in it.

    4. Build and Install the module
      $ cd ODBC/mxODBC
      $ make -f Makefilre.pre.in boot
      $ make
      

Now, you can write a simple program and test it.

3. Your First Python Program

We will write a very simple Python file to retrieve the data in the database test.

Write the following Python code to a file, and name it with the extension name .py (like test01.py).


#!/usr/bin/env python

import ODBC.mxODBC
DB=ODBC.mxODBC
# create Connection object
conn = DB.connect('test', 'SYSADM', '')

# create Cursor object
cursor = conn.cursor()

# execute a SQL statement
cursor.execute('select * from t1')
result = cursor.fetchall()

for i in range(len(result)):
  print result[i]

# close cursor and connection
cursor.close()
conn.close()

4. Testing Your First Python Program

You have two methods to run the program:

  1. Running it as a executable script.
    You can running it as a executable script by changing the asscess mode of the testing program to executable:
    $ chmod a+x test01.py
    $ ./test01.py
    (1, 'dbmaker   ')
    (2, 'database  ')
    
    This allows all people can execute the program from a shell directly.

    NOTE: Your Python programs must have this line

    #!/usr/bin/env python
    
    as the first code line (not a comment line) to make this method workable.

  2. Running it as a Python program
    Another way is executing the program by the python program:
    $ python test01.py
    $ (1, 'dbmaker   ')
    (2, 'database  ')
    
    Using this method, you don't have to write the line
    #!/usr/bin/env python
    
    at the beginning of your programs.

5. More Information

The following are Python related information.

Sites

  1. Python Home
  2. Zope Home

Documents

  1. Python FAQ
  2. Python Database API Specification 2.0

Packages

  1. mxODBC module
  2. mxDateTime module (needed by mxODBC)
  3. Zope ODBC database adaptor v3.1.0

[ Back To Manuals Index ]

Copyright 2002 SYSCOM Computer Engineering Co. All rights reserved.