Python database and Apache module integration notes

Source: Internet
Author: User

1. DB2 (1) first download the driver: (pydb2)
Http://sourceforge.net/projects/pydb2/

(2) installation:
Execute the exeinstallation program, such as pydb2-1.1.1-1.win32-py2.5.exe.
For Linux, run
Python setup. py build
Python setup. py install

(3) test
Import DB2

Conn = db2.connect (DSN = 'gzdmt', uid = 'gzcrm ', Pwd = '1jcsxdl ')
Curs = conn. cursor ()
SQL = 'select * From dim_pub_city'
Rrw.curs.exe cute (SQL)
Row = curs. fetchone ()
While row:
(City_id, city_name) = (row [0], row [1])
Row = curs. fetchone ()
Print city_id, city_name
Curs. Close ()

Note: It is strange that pydb2 does not provide the conn. Close () method and does not know how it closes the connection.

(4) Others
In Linux, Python libdb2.so. 1 may not be found.
Solution:
# Vi/etc/ld. conf
/Usr/kerberos/lib
/Usr/X11R6/lib
// Usr/lib/qt-3.1/lib
/Usr/lib/sane
/Usr/local/mysql/lib/mysql
/Opt/IBM/db2/V8.1/lib/

# Ldconfig

 

Ii. Oracle

(1) first download the driver: (cx_Oracle)
Http://www.python.net/crew/atuining/cx_Oracle/

(2) installation:
First, configure the oracle_home Environment Variable
Execute the exeinstallation program, such as cx_oracle-4.3.1-win32-10g-py25.exe. It will copy a cx_Oracle.pyd to the Lib/site-packages directory.
For linux, run
Python setup. py build
Python setup. py install

(3) test
Import cx_Oracle

"" Conn = cx_Oracle.connect ("aiomni", "aiomni_123", "kpi_36 ")"""
Conn = cx_Oracle.connect ('aiomni/aiomni_123 @ kpi_36 ')
Curs = conn. cursor ()
SQL = 'select * from dim_pub_city'
Rrw.curs.exe cute (SQL)
Row = curs. fetchone ()
While row:
(City_id, city_name) = (row [0], row [1])
Row = curs. fetchone ()
Print city_id, city_name
Curs. close ()
Conn. close ()

(4) on the Internet, it is sometimes said that there may be problems on the window platform, and my eclipse + Pydev environment is not encountered. The following is a list of the issues that may be helpful in the future.

A. When you import cx_Oracle, the OCI. DLL cannot be found:
Find one on the machine where Oracle is installed, and copy it to the Libsite-packages directory.

B. Report RuntimeError: Unable to acquire Oracle environment handle when cx_Oracle.connect:
This is troublesome and can be solved by following the steps below: (all steps may not be required. I did not confirm it, but I have executed all the steps below and solved the problem)
First, make sure that you are running the python script under the console. Instead of some ides, such as PyDev (they do not seem to be able to load OS environment variables ).
In fact, install Oracle on the local machine (only the client tool can be installed ).
Finally, add the following environment variables: (I will give my environment variables and change them to your own path)
ORACLE_HOME = D: OracleOra81
PATH = D: OracleOra81bin;

 

Iii. MySQL

(1) first download the driver: (MySQLdb)
Http://mysql-python.sourceforge.net/

(2) installation:
Execute the exeinstallation program, such as mysql-python-1.2.2.win32-py2.5.exe.
For linux, run
Python setup. py build
Python setup. py install

(3) test
Import MySQLdb

Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'asiainfo', db = 'test ')
Curs = conn. cursor ()
SQL = 'select * from orders'
Rrw.curs.exe cute (SQL)
Row = curs. fetchone ()
While row:
(Id, order_number) = (row [0], row [1])
Row = curs. fetchone ()
Print id, order_number
Curs. close ()
Conn. close ()

Iv. Integration with Apache

(1) download
Apache:
Http://httpd.apache.org/download.cgi

Mod_python_apache:
Http://apache.mirror.phpchina.com/httpd/modpython/win/3.3.1/

(2) Installation
Click mod_python-3.3.1.win32-py2.5-apache2.2.exe to install apache.

(3) edit the httpd. conf file of apache and add the following lines.

LoadModule python_module modules/mod_python.so
AddHandler mod_python. py
PythonHandler mod_python.psp
PythonDebug On

(4) Restart apache and edit the test file in the C:/Apache2.2/htdocs directory.

Test. py

<%
For I in range (5 ):
J = 2 ** I
# Loop block begins
%>
<% = "% D <sup> % d </sup> = % d" % (2, I, j) %>
<%
# This will close the loop block
%>

(5) access address:

Http: // localhost/test. py

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.