Python connects Oracle database instance _python

Source: Internet
Author: User
Tags python script

This article is an example of how Python connects Oracle databases and shares them for your reference. The specific steps are as follows:

First, download the driver: (cx_oracle)

http://www.python.net/crew/atuining/cx_Oracle/
But pay attention to the version and choose it according to your situation.

Second, installation:

First configure the ORACLE_HOME environment variable
Execute that EXE installer on it, it will copy a cx_oracle.pyd to the Libsite-packages directory.
If it is Linux, perform

Copy Code code as follows:
Python setup.py Build
Python setup.py Install

Third, the implementation of a test procedure:

Copy Code code as follows:
Import Cx_oracle

con = cx_oracle.connect ("Xjtu_test", "37343734", "Xjtu.world")
cursor = Con.cursor ()
Cursor.close ()
Con.close ()


The 3 parameters in connect are left to right: User, pass, TNS.
That TNS can be configured with the net Configuration Assistant in the Oracle Client tool.

Four, the specific Cx_oracle API can refer to:
Http://www.python.net/crew/atuining/cx_Oracle/html/cx_Oracle.html

V. Examples:

Copy Code code as follows:
>>> Import Cx_oracle
>>> conn=cx_oracle.connect (' scott/tiger@oratest ')
>>> Curs=conn.cursor ()
>>> sql= ' select * from emp '
>>> rr=curs.execute (SQL)
>>> Row=curs.fetchone ()
>>> row
(7369, ' SMITH ', ' clerk ', 7902, Datetime.datetime (1980, 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 use the Windows platform, you must have a problem executing that test code, and you will generally have the following questions:

①import cx_oracle report cannot find OCI.DLL:
Find one on the Oracle machine and copy it to the Libsite-packages directory.

②cx_oracle.connect report runtimeerror:unable to acquire Oracle environment handle:
This is more cumbersome, follow these steps to resolve: (may not need all the steps, I did not confirm, but the following steps are executed, the problem is solved)
First, make sure you're executing this python script underneath the console. Rather than some Ides, such as PyDev (they don't seem to load the OS's environment variables).
In fact, install Oracle on this machine (only the client tools are available).
Finally, add the following environment variables: (I give my, change your own path is OK)

Copy Code code as follows:
Oracle_home=d:oracleora81
Path=d:oracleora81bin;

I hope this article will help you with your Python programming.

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.