引言
cx_Oracle是Python環(huán)境下的一個(好像也是唯一的一個)用于操作Oracle的第三方模塊。
最近在寫某個對帳程序時,不得已要連接一臺Oracle庫,于是用到了cx_Oracle,總的來說還是比較順利的,期間遇到幾個有意思的小坑寫出來分享一下。
RHEL 6.4下安裝cx_Oracle
RHEL6.4和cx_Oracle比較搭,安裝時應(yīng)該不會遇到什么挫折,使用RPM安裝好instantclient后,直接用pip3安裝cx_Oracle即可。
需要注意地方:
- 盡量使用RPM方式安裝instantclient,安裝更方便,而且比zip更好管理。
- instantclient的版本選擇的是11.2.0.4.0,沒有選擇12。對于Oracle/WebLogic這類閉源的東西,還是選擇次最新版本的比較穩(wěn)妥。
安裝步驟:
下載Linux版本的instantclient
這里是 Oracle官網(wǎng) instantclient下載頁面 ,下載以下3個RPM包:
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm安裝instantclient并設(shè)置環(huán)境變量
yum -y install libaio bc flex
rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
- 設(shè)置環(huán)境變量
echo 'export ORACLE_VERSION="11.2"' >> $HOME/.bashrc
echo 'export ORACLE_HOME="/usr/lib/oracle/$ORACLE_VERSION/client64/"' >> $HOME/.bashrc
echo 'export PATH=$PATH:"$ORACLE_HOME/bin"' >> $HOME/.bashrc
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$ORACLE_HOME/lib"' >> $HOME/.bashrc
source $HOME/.bashrc
- 使用pip安裝cx_Oracle
pip3 install cx_Oracle
macOS 12下安裝cx_Oracle
macOS下,安裝cx_Oracle后的編譯過程有可能會報錯,一般是因為instantclient安裝有誤造成的。
需要注意的地方:
- mac下的instantclient只有zip包一種安裝方式,要注意手工建兩個軟鏈接。
- pip安裝前,注意導(dǎo)入LD_LIBRARY_PATH與DYLD_LIBRARY_PATH兩個環(huán)境變量。
安裝過程:
下載Mac版本的instantclient
下載以下3個zip包,并unzip
解壓至同一目錄:
instantclient-basic-macos.x64-11.2.0.4.0.zip
instantclient-sdk-macos.x64-11.2.0.4.0.zip
instantclient-sqlplus-macos.x64-11.2.0.4.0.zip建立軟鏈接
cd /path/to/instant
ln -s libclntsh.dylib.11.2 libclntsh.dylib
ln -s libocci.dylib.11.2 libocci.dylib
- 設(shè)置環(huán)境變量
export ORACLE_VERSION="11.2"
export ORACLE_HOME="/path/to/instantclient_11_2"
export PATH=$PATH:"$ORACLE_HOME"
- 使用pip安裝cx_Oracle,注意提前導(dǎo)入DYLD_LIBRARY_PATH與LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH="$ORACLE_HOME"
export LD_LIBRARY_PATH="$ORACLE_HOME"
pip3 install cx_Oracle
中文亂碼問題
Oracle中文亂碼問題存在已久,使用cx_Oracle時也不例外,解決方法還是設(shè)置NLS_LANG環(huán)境變量。
有兩種方式,一是在系統(tǒng)中設(shè)置永久環(huán)境變量,二是直接在代碼中使用os.environ
設(shè)置環(huán)境變量,這里推薦后者。
- 方法一:在Shell中設(shè)置環(huán)境變量
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.UTF8"
- 方法二:直接在代碼中加入:
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
PyCharm下不識別cx_Oracle問題
安裝安成后,在python的console中已經(jīng)可以import cx_Oracle
了,但在PyCharm中卻提示找不到cx_Oracle。這是一個比較大的坑,可以詳細講一下處理過程。
首先初步定位到原因,是由于PyCharm中沒有定義LD_LIBRARY_PATH與DYLD_LIBRARY_PATH兩個環(huán)境變量造成的。
PyCharm會自動讀取系統(tǒng)中的環(huán)境變量設(shè)置并導(dǎo)入,但唯獨這兩個沒有導(dǎo)進來。不深究原因,先嘗試手工在PyCharm中配置這兩個環(huán)境變量,總共有兩處可以配置:
第一處:
CMD+,
打開Preference,找到 Build,Execution,Deployment -> Console -> Python Console ->Enviroment Variables
此處的配置會修復(fù)PyCharm中的Console。
第二處:
右上角 Run -> Edit Configurations,添加兩條環(huán)境變量
此處的配置會修復(fù)PyCharm中
Ctrl+Shift+R
運行代碼時的報錯。
在以上兩處手工添加環(huán)境變量:
DYLD_LIBRARY_PATH=/path/to/instantclient
LD_LIBRARY_PATH/path/to/instantclient
兩處的環(huán)境變量配置完成后,雖然console中可以正常使用cx_Oracle了,代碼也可以正常運行了,但編輯界面中的inspection還是有問題的,提示有Error,并且不能使用自動完成功能。
于是回到之前的問題,為什么明明已經(jīng)定義了,但PyCharm卻沒有找到LD_LIBRARY_PATH與DYLD_LIBRARY_PATH?
同時偶然發(fā)現(xiàn),在執(zhí)行env
命令查看已定義的環(huán)境變量時,也是找不到LD_LIBRARY_PATH與DYLD_LIBRARY_PATH的。
這貌似已經(jīng)不是PyCharm自身的問題了,需要從macOS系統(tǒng)來著手了。
于是在stackoverflow上找到了這么一段話:
El Capitan added system integrity protection (SIP), and one side effect of that is that exporting DYLD_LIBRARY_PATH doesn't work. That could affect running SQL*Plus from a shell script, for example. There are workarounds for the 11g instant client. The installation notes at the bottom of the download page have changed since I last did this, and it now says to hard link the library files to the user's ~/lib directory to avoid that issue. Fortunately it looks like you don't need to worry about that with the 12c client - they've fixed the way it's built.
看來根本原因是OSX 10.11之后加入的這個SIP引發(fā)的了。
首先想到的最簡單的方法,將所有的.dylib
和.h
都拷貝到系統(tǒng)默認的目錄就可以了。
但很杯具,/usr/lib
和/usr/include
這兩個目錄也被SIP保護了。因為不想強制關(guān)閉SIP,繼續(xù)再想別的辦法。
接著發(fā)現(xiàn)/usr/local/lib
和/usr/local/include
目錄還是可以操作的,于是拷到這兩個目錄:
cd /path/to/instantclient
cp *.dylib /usr/local/lib
cp ./sdk/include/*.h /usr/local/include
再將cx_Oracle卸載后重新編譯安裝:
sudo pip3 uninstall cx_Oracle
sudo pip3 install cx_Oracle
重啟PyCharm后,問題解決。
cx_Oracle的簡單使用
cx_Oracle的使用上沒有什么問題,一切按套路來寫就可以了。貼一個簡單的示例:
import cx_Oracle
conn = cx_Oracle.connect('username', 'password','host:port/sid')
cursor = conn.cursor()
cursor.execute('select column from table')
result = cursor.fetchall()
for row in result:
print(row)
cursor.close()
conn.close()
更加詳細的內(nèi)容請參考官方文檔:
https://cx-oracle.readthedocs.io/en/latest/
https://oracle.github.io/python-cx_Oracle/
參考文檔
https://gist.github.com/thom-nic/6011715
http://stackoverflow.com/questions/37711482/how-to-install-oracle-instant-client-on-a-mac