Connect to the Oracle database in Python

Source: Internet
Author: User

It took another morning to configure this item and write it to reduce your detours. My environment is WINXP SP2 + python2.4 + Oracle 8i.

[1] first download the driver: (cx_oracle)
Http://www.python.net/crew/atuining/cx_Oracle/
But pay attention to the version and select it based on your situation.
[2] installation:
Run the EXE installer and copy a cx_oracle.pyd to the {python_home} \ Lib \ Site-packages directory.
[3] run a test program:

Import cx_oracle

Con = cx_oracle.connect ("xjtu_test", "37343734", "xjtu. World ")
Cursor = con. cursor ()
Cursor. Close ()
Con. Close ()

The three parameters in connect are user, pass, and TNS from left to right.
The TNS can be configured using the net configuration Assistant in the Oracle client tool.
[4] for specific cx_oracle APIs, refer:
Http://www.python.net/crew/atuining/cx_Oracle/html/cx_Oracle.html

Okay. You must have encountered a problem when executing the test code. The following problems may occur:
[1] OCI. dll cannot be found in the report when you import cx_oracle:
Find one on the machine with Oracle installed, and copy it to the {python_home} \ Lib \ Site-packages directory.
[2] runtimeerror: unable to acquire Oracle environment handle reported during 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: \ oracle \ ora81
Path = D: \ oracle \ ora81 \ bin; {your_other_paths}

I 've been stuck in pydev for a long time, FT ~, Then switch to the console and everything will be fine ,:-)

Related Article

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.