Python connects to Oracle database
1, installation Cx_oracle
Pip Install Cx_oracle
2. Appearance
Cx_oracle.databaseerror:dpi-1047:64-bit Oracle Client Library cannot is loaded: "Dlopen (Libclntsh.dylib, 1): Image not F Ound ". See Https://oracle.github.io/odpi/doc/installation.html#macos for help
Workaround for Error:
Download the 64bit client basic and Client SDK for Mac version on Oracle website
Http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
Compile and install:
sudo su #切换到root用户
Mkdir/users/guanguan/oracle #创建oracle文件
mv/users/guanguan/downloads/instantclient-*/users/guanguan/oracle #将下载的两个Oracle包放到/users/guanguan/oracle Directory
Cd/users/guanguan/oracle into the Oracle file
Unzip Instantclient-basic-macos.64-12.1.0.4.0.zip #解压
Unzip Instantclient-sdk-macos.64-12.1.0.4.0.zip #解压
CD INSTANTCLIENT_12_1/SDK
Unzip Ottclasses.zip
Cd..
Cp-r./sdk/*.
Cp-r./sdk/include.
Ln-s libocci.dylib.12.1 Libocci.dylib
Ln-s libclntsh.dylib.12.1 Libclntsh.dylib
To change an environment variable:
VI ~/.bash_profile
Export Oracle_home=/users/guanguan/oracle/instantclient_12_1
Export Dyld_library_path= $ORACLE _home
Export Ld_library_path= $ORACLE _home
Then enter source ~/.bash_profile or. ~/.bash_profile Making environment variables effective
5) Whether the test environment variable is in effect
Echo $ORACLE _home
/users/guanguan/oracle/instantclient_12_1
6) Then unzip the installation cx_oracle:
TAR-ZXVF cx_oracle-5.2.1.tar.gz
CD cx_oracle-5.2.1
Python setup.py Build
Python setup.py Install
7) Test Cx_oracle installation is successful
Python
Import Cx_oracle
#运行结果结果:
? ~ Python python 2.7.10 (default, OCT, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on DARW In Type ' help ', ' copyright ', ' credits ' or ' license ' for more information. >>> Import cx_oracle >>>
or python-c "Import cx_oracle"
? ~ python-c "Import cx_oracle"? ~
This means that the installation is successful.
Error message:
sh-3.2# python-c "Import cx_oracle"/library/python/2.7/site-packages/cx_oracle-5.2.1-py2.7-macosx-10.11-intel.egg /cx_oracle.py:3: Userwarning:module cx_oracle was already imported from/library/python/2.7/site-packages/cx_ ORACLE-5.2.1-PY2.7-MACOSX-10.11-INTEL.EGG/CX_ORACLE.PYC, but/users/guanguan/oracle/cx_oracle-5.2.1 is being added To Sys.path Traceback (most recent): File "", line 1, in File "build/bdist.macosx-10.11-intel/egg/cx_oracle.py", Line 7, in File "build/bdist.macosx-10.11-intel/egg/cx_oracle.py", line 6, in __bootstrap__ Importerror:dlopen (/var/ Root/.python-eggs/cx_oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_oracle.so, 2): Library not loaded: @rpath/ Libclntsh.dylib.12.1referenced from:/var/root/.python-eggs/cx_oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_ Oracle.soReason:image not found
Workaround: (Remove the previously installed cx_oracle, set export force_rpath=true, reinstall Cx_oracle)
sh-3.2# Export force_rpath=true sh-3.2# pip install cx_oracle requirement already satisfied:cx_oracle in/library/python/ 2.7/site-packages/cx_oracle-5.2.1-py2.7-macosx-10.11-intel.egg sh-3.2# cd/library/python/2.7/site-packages/ sh-3.2# rm-f Cx_oracle-5.2.1-py2.7-macosx-10.11-intel.egg sh-3.2# pip install cx_oracle collecting cx_OracleUsing Cached cx_oracle-5.2.1.tar.gz Installing collected packages:cx-oraclerunning setup.py install for cx-oracle ... done SUCC Essfully installed cx-oracle-5.2.1
sh-3.2# python python 2.7.10 (default, OCT, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] O N Darwin Type "Help", "copyright", "credits" or "license" for more information. >>> Import cx_oracle >>> exit ()
==========================================
Python's simple operation for cx_oracle:
#! /usr/bin/python
Import Cx_oracle
Dsnstr = Cx_oracle.makedsn ("127.0.0.1", "1521", "ORCL")
conn = Cx_oracle.connect (user= "test", password= "test", DSN=DSNSTR)
C=conn.cursor ()
X=c.execute (' Select *from TEST. TEST p WHERE id<2 ')
Print (X.fetchone ())
C.close ()
Conn.close ()
Python learning-Interacting with Oracle