Connect python to an oracle database instance and a pythonoracle instance

Source: Internet
Author: User

Connect python to an oracle database instance and a pythonoracle instance

This article describes how to connect python to the oracle database. The procedure is as follows:

I. 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.

Ii. installation:

First, configure the oracle_home Environment Variable
Run the exe installer and copy a cx_Oracle.pyd to the Libsite-packages directory.
For linux, run
Copy codeThe Code is as follows: python setup. py build
Python setup. py install

3. Execute a test program:
Copy codeThe Code is as follows: 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

V. Example:
Copy codeThe Code is as follows: >>> import cx_Oracle
>>> Conn = cx_Oracle.connect ('Scott/tiger @ oratest ')
>>> Curs = conn. cursor ()
>>> SQL = 'select * from emp'
>>> Rr1_curs.exe cute (SQL)
>>> Row = curs. fetchone ()
>>> Row
(7369, 'Smith ', 'cler', 7902, datetime. datetime (1980, 12, 17, 0, 0), 800.0, None, 20)
>>> While row:
(ID, NAME) = (row [0], row [1])
Row = curs. fetchone ()
Print ID, NAME


7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 daily
7934 MILLER

If you are using a windows platform, you must have encountered a problem when executing that test code. Generally, the following problems occur:

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

② 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)
Copy codeThe Code is as follows: ORACLE_HOME = D: OracleOra81
PATH = D: OracleOra81bin;

I hope this article will help you with Python programming.


How does sqlplus connect to an oracle database instance?

Conn sys/password @ orcl as sysdba
Conn sys/password @ myorcl as sysdba

In the oracle database, if I have two instances, ORCL and MYDB, how can I connect to the specified instance by default ORCL, but I want to connect to MYDB?

What is the connection?
If sqlplus is connected, set the environment variable set ORACLE_SID = MYDB In the cmd window.

Nothing else.

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.