Installation and Use of the pythoncx_Oracle module

Source: Internet
Author: User
This article describes how to install and use the pythoncx_Oracle module. For more information, see The python cx_Oracle module installation.

Recently, you need to write a data migration script to migrate data from a single Oracle database to a MySQL Sharding cluster. it is still a little troublesome to install cx_Oracle in linux.

For the Oracle Client, you not only need to install the corresponding python module (here I use the official python module of Oracle -- cx_Oracle), but also need to install the Oracle Client. generally, it is enough to select Instant Client, you also need to configure tnsnames. ora (you can also simply access it through host: port/schema ).

Installation:

1. determine the version first. Because our Oracle data is a little old, I chose an old version-Oracle Instant Client 10.2.0.4.

2. download instantclient-basic. : Http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html .. This is a serious issue for BS Oracle. registration is required before downloading. The key is that the password must contain numbers, letters, and uppercase/lowercase letters during registration, it must have at least 8 digits. I am forced to create a password that is more secure than my bank password (well, now I forgot what I entered...), just go to basic.

$wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip

3. install and configure

$ Unzip instantclient-basic-linux.x64-10.2.0.4.0.zip $ cd instantclient_10_2 $ cp */usr/lib # put directly into the dynamic library search path, no additional environment configuration or $ unzip instantclient-basic-linux.x64-10.2.0.4.0.zip $ cp-rf instantclient_10_2/opt/$ vi/etc/profile export ORACLE_HOME =/opt/instantclient_10_2 export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH: $ ORACLE_HOME $ source/etc/profile

4. configure tnsnames. ora (tns is not required)

Note that tnsnames. ora does not actually exist. it is created by myself (this is also disgusting. At first I thought it was necessary to install something else ..), I didn't use this method. if you are interested, you can google it.

5. download and install the cx_Oracle python module

$ Wget plugin-ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm $ ls/usr/lib/python2.6/site-packages/cx_Oracle.so # if this file is available, the installation is successful, depending on the location of python, or elsewhere, find it by yourself

6. verify and solve the problem

$python>>import cx_Oracle

If the error is returned: import cx_Oracle gave ImportError: libclntsh. so.10.1: cannot open shared object file: No such file or directory

Indicates that the dynamic library of the instant client is not found. check whether the environment variables are configured, effective, and the version is correct.

If the error message is: ImportError:./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode

Google information: There is nothing wrong with Debian. python supports two incompatible modes of operation for Unicode, UCS2 (the default), and UCS4. Debian uses the default, Redhat uses UCS4. You need to recompile the extension for the UCS-2 mode (I. e. using a Debian installation); this wocould fix the undefined symbol: PyUnicodeUCS4_Decode

So re-compile python

$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4$make;make install

Verify again and finally import normally.

Usage:

1. basic connection-use Oracle tns alias

Connection = cx_Oracle.connect ("tp/tp @ ocn_test") # view the tns alias command cmd> tnsping ocn_testTNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110: 47: 48 Copyright (c) 1997,200 6, Oracle Corporation. allrights reserved. used parameter files:/opt /...... /Sqlnet. oraUsed TNSNAMES adapter to resolve the aliasAttempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 10.20.36.19) (PORT = 1520 ))) (CONNECT_DATA = (SID = ocntest) OK (10 msec)

2. the user enters the password to connect

pwd =getpass.getpass()connection =cx_Oracle.connect("tp",pwd,"ocn_test")

3. the user directly enters the connection account information in the Python command, in the format of python script. py tp/tp @ ocn_test

connection =cx_Oracle.connect(sys.argv[1])

4. use the Easy Connect syntax to Connect to the database through Drive

connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest')#orconnection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')

5. use DSN to create TNSNAME first

tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')connection =cx_Oracle.connect('tp','tp',tns_name)

6. log on to the as SYSDBA

connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)#or as SYSOPERconnection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)

An error is reported when you perform the Oracle operation on the Linux server:

TNS:listener does not currently know of service requested in connect descriptor

Solution:

For more information, see http://ora-12514.ora-code.com.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more information about how to install and use the python cx_Oracle module, see the Chinese PHP website!

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.