$ip=getenv(REMOTE_ADDR);
?>
DBMaker Starting Guides
The Starting Guide for Python
We suppose that you have the basic concept about Python. For more information
about Python, please reference the
Python home site.
- 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.
- Install the ODBC driver
Please follow these steps to install mxODBC driver:
- 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.
- Build and install the module.
$ cd DateTime/mxDateTime
$ make -f Makefile.pre.in boot
$ make
- 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.
- 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.
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()
You have two methods to run the program:
- 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.
- 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.
The following are Python related information.
- Sites
-
- Python Home
- Zope Home
- Documents
-
- Python FAQ
- Python
Database API Specification 2.0
- Packages
-
- mxODBC
module
- mxDateTime
module (needed by mxODBC)
- Zope
ODBC database adaptor v3.1.0
[ Back To Manuals Index ]
|