Install the cx_Oracle module in Python

Source: Internet
Author: User

Install the cx_Oracle module in Python

If you want to access a remote Oracle database and do not want to install a few hundred megabytes of Oracle Client locally (also known as root), the python cx_Oralce module must rely on the Oracle Instant Client to replace the Complete Oracle Client.

Oracle Instant Client download: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

Download:

  1. instantclient-basic-linux.x64-11.2.0.4.0.zip
  2. instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
  3. instantclient-sdk-linux.x64-11.2.0.4.0.zip

Decompress the file to the following directory:

$HOME/oracle/instantclient_11_2

Copy tnsnames. ora file, create the network/admin directory under the/home/oracle/instantclient_11_2 directory, and set tnsnames. copy the ora file. (This does not seem to be a necessary condition. If something goes wrong, add it)

~ /. Bashrc settings:

export ORACLE_HOME=$HOME/oracle/instantclient_11_2export TNS_ADMIN=$ORACLE_HOME/network/adminexport DYLD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$ORACLE_HOME:$PATH

The following figure shows the python module for installing cx_Oracle. Download python setup. py install directly.

If the permission is not root, NO/lib64/python2.6/site-packages/directory read/write permission is reported.

You can rewrite the python installation directory.

export PATH=$ORACLE_HOME:$PATH

Then source. bashrc takes effect

Next, you can install python setup. py install -- prefix = ~ /. Local (the prefix of the local directory is directly added to the original directory, Lili's solution to non-root barriers)

Let it go and report an error (crash),/usr/bin/ld: cannot find-lclntsh

This error was not encountered when cx_Oracle was installed on mac. Baidu's solution found that the libclntsh. so function library under lib was not found.

There are actually some, but the name is changed and soft links are added:

Ln-s libclntsh. so.11.1 libclntsh. so

Install again.

Supplement:

Non-root rpm package installation

First, extract the RPM package, put it in your own directory, and add environment variables.
The decompressed command is:
rpm2cpio ctags-5.8-2.el6.x86_64.rpm | cpio -idvm
In this way, the directory structure in the package will be decompressed to the current directory. If it is a home directory, you can add environment variables in. bashrc in the home directory.

  1. vim ~/.bashrc
  2. export PATH=$PATH:$HOME/usr/bin/

Log On again orsource ~/.bashrcFile, you can use this program.

Easy_install -- prefix = ~ /. Local cx_Oracle

Uninstall:

python setup.py install --record record.txt --prefix=~/.local
Then deleterecord.txtAll files in

The post section cx_Oracle uses code as a memo:

Import cx_Oracle
Class ConnectOracle:
Def _ init _ (self, username, passwd, locate ):
Self. login = {}
Self. db = None
Self. cursor = None
Self. login ['username'] = username
Self. login ['passwd'] = passwd
Self. login ['locate'] = locate

Def connect_oracle (self ):
Try:
Self. db = cx_Oracle.connect (self. login ['username'], self. login ['passwd'], self. login ['locate']) # log on to the internal database to search
Self. db. autocommit = False # disable automatic submission
Self. cursor = self. db. cursor () # Set the cursor
Return True
Except t:
Print 'can not connect oracle'
Return False

Def close_oracle (self ):
Self. cursor. close ()
Self. db. close ()

Def select_oracle (self, SQL, num = 0, temp = None ):
If self. connect_oracle ():
If temp:
Self.cursor.exe cute.pdf (SQL, temp)
Else:
Self.cursor.exe cute (SQL)
If num:
Content = self. cursor. fetchmany (num)
Else:
Content = self. cursor. fetchall ()
Self. close_oracle ()
Return content
Return False

Def insert_oracle (self, SQL, temp = None ):
Try:
Self. connect_oracle ()
If temp:
Self.cursor.exe cute.pdf (SQL, temp)
# Execute multiple SQL commands
Else:
Self.cursor.exe cute (SQL)
Except t:
Print "insert exception"
Self. db. rollback () # rollback
Finally:
Self. db. commit ()
Self. close_oracle ()

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.