一、安装Oracle客户端
1、下载对应安装文件,官网地址:http://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html
这个是我自己下载好的:https://pan.baidu.com/s/10vbwHkvOrYRH3ewdkBuioQ
2、把下载好的文件放到Linux中
3、执行安装命令,因sdk包下的是压缩文件,所以直接解压放在安装目录,默认安装目录是/usr/lib/oracle/下
rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
4、设置环境变量
(1)编辑root用户下的profile文件
vi /etc/profile
(2)在文件的最后添加以下内容
export ORACLE_HOME=/usr/lib/oracle/11.2/client64 export TNS_ADMIN=$ORACLE_HOME/network/admin export NLS_LANG='simplified chinese_china'.ZHS16GBK export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH export PATH=$ORACLE_HOME/bin:$PATH
(3)执行source命令,使配置文件生效
source /etc/profile
二、安装cx_Oracle
ps:我是用pip安装的,因刚开始用非pip方式安装时,遇到如下错误,当时查了半天资料,说什么的都有,有说是Oracle的path配置不对,试过,好像没管用,具体没深究。
http://sourceforge.net/projects/cx-oracle/files/ cx_ORacle 旧版下载地址
https://pypi.org/project/cx_Oracle/#history cx_Oracle 新版下载地址
find / -name 'cx_Oracle.so' -print --查找文件命令
rpm -qa | grep -i cx_Oracle --查看安装的cx_Oracle得rmp
rpm -e cx_Oracle-5.1.2-1.x86_64 --卸载rmp
Traceback (most recent call last): File "setup.py", line 187, in <module> raise DistutilsSetupError("cannot locate Oracle include files") distutils.errors.DistutilsSetupError: cannot locate Oracle include files
1、安装Python 的pip
(1)、下载文件,需要网络,也可以提前下载,放到指定目录。
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
(2)、执行安装
python get-pip.py
2、用pip安装cx_Oracle
(1)、安装cx_Oracle之前需要先建立一个链接libclntsh.so,如下:
cd /usr/lib/oracle/11.2/client64
ln -s libclntsh.so.11.1 libclntsh.so
(2)、执行安装
pip install cx_Oracle
(3)、测试,导入import cx_Oracle不报错,即说明安装成功,然后再测试连接数据库
import cx_Oracle
import cx_Oracle conn = cx_Oracle.connect('jhinno/jhinno@192.168.0.188/jhinno') cursor = conn.cursor () cursor.execute ("select sysdate from dual") row = cursor.fetchone () print row cursor.close () conn.close ()
三、封装的简单的类
#!/usr/bin/env python #coding:utf-8 import cx_Oracle #查询数据库 def sqlSelect(sql,db,*params): cr=db.cursor() cr.execute(sql,*params) rs=cr.fetchall() cr.close() return rs #增、删、改 def sqlDML(sql,db,*params): cr=db.cursor() cr.execute(sql,*params) cr.close() db.commit() #批量插入 def sqlInsertSelect(sql,db,params): cr=db.cursor() cr.executemany(sql,params) cr.close() db.commit() if __name__ =='__main__': #查询数据1 empno="in ('7369','7499','7521')" sql="select * from scott.emp a where a.empno %s" %empno rows=db.sqlSelect(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl')) #查询数据2 #params = {'empno':'7499'} #sql="select * from scott.emp a where a.empno in:empno" #rows=db.sqlSelect(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'), params) for row in rows: print row #批量插入数据(将查询的scott.emp数据插入到与之表结构一样的另一张表scott.emp_test) sql="insert into scott.emp_test VALUES(:1,:2,:3,:4,:5,:6,:7,:8)" db.sqlInsertSelect(sql,cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'),rows) #删除 empno="in ('7369','7499','7521')" sql="delete from scott.emp a where a.empno %s" %empno db.sqlDML(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl')) #修改 empno="in ('7369','7499','7521')" value='IT' sql="update scott.emp a set a.job= %s where a.empno %s" %(value,empno) db.sqlDML(sql, cx_Oracle.connect('scott/scott@192.168.1.12:1521/orcl'))