Python 安装cx_Oracle模块
想访问远程Oracle数据库,本地又不想安装几百兆的Oracle Client(也木有root权限),安装python的cx_Oralce 模块需要依赖Oracle Instant Client 代替完整的Oracle Client。
Oracle Instant Client 下载:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
下载:
<span class="pln">instantclient<span class="pun">-<span class="pln">basic<span class="pun">-<span class="pln">linux<span class="pun">.<span class="pln">x64<span class="pun">-<span class="lit"><span class="hljs-number">11.2<span class="pun">.<span class="lit"><span class="hljs-number">0.4<span class="pun">.<span class="lit"><span class="hljs-number">0.zip</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">instantclient<span class="pun">-<span class="pln">sqlplus<span class="pun">-<span class="pln">linux<span class="pun">.<span class="pln">x64<span class="pun">-<span class="lit"><span class="hljs-number">11.2<span class="pun">.<span class="lit"><span class="hljs-number">0.4<span class="pun">.<span class="lit"><span class="hljs-number">0.zip</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">instantclient<span class="pun">-<span class="pln">sdk<span class="pun">-<span class="pln">linux<span class="pun">.<span class="pln">x64<span class="pun">-<span class="lit"><span class="hljs-number">11.2<span class="pun">.<span class="lit"><span class="hljs-number">0.4<span class="pun">.<span class="lit"><span class="hljs-number">0.zip</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
解压文件放到如下目录:
$HOME/oracle/instantclient_11_2
拷贝tnsnames.ora文件,在/home/oracle/instantclient_11_2目录下创建network/admin目录,并将tnsnames.ora文件拷贝进去(这个貌似不是必要条件,出了问题的话就补上吧)
~/.bashrc设置:
export ORACLE_HOME=$HOME/oracle/instantclient_11_2 export TNS_ADMIN=$ORACLE_HOME/network/admin export DYLD_LIBRARY_PATH=$ORACLE_HOME export PATH=$ORACLE_HOME:$PATH
下面就是该安装cx_Oracle的python模块了,下载直接python setup.py install
由于非root的权限,会报无/lib64/python2.6/site-packages/目录读写权限
可以改写python的安装目录
export PATH=$ORACLE_HOME:$PATH
然后source .bashrc生效
接下来可以安装了:python setup.py install --prefix=~/.local (牛逼闪闪啊,直接在原目录前加了个本地目录前缀,华丽丽的解决非root的壁垒)
顺其自然的又报错了(奔溃呀),/usr/bin/ld: cannot find -lclntsh
这个错误之前在mac上装cx_Oracle时候没遇到,百度解决方案,得知是没有找到lib下的libclntsh.so函数库
其实是有的,只不过名字改了,加软链接:
ln -s libclntsh.so.11.1 libclntsh.so
再次安装解决。
补充:
非root安装rpm包
首先把RPM包解压出来,然后放在自己的目录下,并且添加好环境变量
解压的命令为:rpm2cpio ctags-5.8-2.el6.x86_64.rpm | cpio -idvm
这样就会按包里的目录结构解压到当前目录,如果是家目录的话,可以在家目录下的.bashrc这样添加环境变量
<span class="pln">vim <span class="pun">~/.<span class="pln">bashrc</span></span></span>
<span class="pln"><span class="hljs-keyword">export PATH<span class="pun">=<span class="pln"><span class="hljs-variable">$PATH<span class="pun">:<span class="pln"><span class="hljs-variable">$HOME<span class="pun">/<span class="pln">usr<span class="pun">/<span class="pln">bin<span class="pun">/</span></span></span></span></span></span></span></span></span></span></span></span></span>
重新登录或者source ~/.bashrc
文件,就可以使用这个程序了
easy_install --prefix=~/.local cx_Oracle
卸载:
python setup.py install --record record.txt --prefix=~/.local
然后删除record.txt
里的所有文件
贴段cx_Oracle 使用代码,作为备忘:
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']) # 登录内搜数据库
self.db.autocommit = False # 关闭自动提交
self.cursor = self.db.cursor() # 设置cursor光标
return True
except:
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.executemany(sql, temp)
else:
self.cursor.execute(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.executemany(sql, temp)
# 执行多条sql命令
else:
self.cursor.execute(sql)
except:
print "insert异常"
self.db.rollback() # 回滚
finally:
self.db.commit()
self.close_oracle()