一、安装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')) 

Python学习之——Oracle数据库连接的更多相关文章

  1. Python学习--和 Oracle 交互(2)

    当在 mac 电脑上用 Python 读取 oracle 数据库中的中文时,有可能返回数据为“?” 解决方案: 在数据库操作的函数前添加以下代码, import sysreload(sys)sys.s ...

  2. Python学习--和 Oracle 交互

    python 连接oracle 数据库 1.安装 cx_oracle pip install cx_oracle 2.出现 cx_Oracle.DatabaseError: DPI-1047: 64- ...

  3. Java学习-006-三种数据库连接 MySQL、Oracle、sqlserver

    此文主要讲述在初学 Java 时,常用的三种数据库 MySQL.Oracle.sqlserver 连接的源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源 ...

  4. 基于python实现Oracle数据库连接查询操作

    使用python语言连接Oracle数据库配置 #coding:utf-8 import cx_Oracle as oracle db=oracle.connect('root/123456@192. ...

  5. [Python] 学习笔记之MySQL数据库操作

    1 Python标准数据库接口DB-API介绍 Python标准数据库接口为 Python DB-API,它为开发人员提供了数据库应用编程接口.Python DB-API支持很多种的数据库,你可以选择 ...

  6. Python学习day5作业

    目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...

  7. Python学习之==>面向对象编程(二)

    一.类的特殊成员 我们在Python学习之==>面向对象编程(一)中已经介绍过了构造方法和析构方法,构造方法是在实例化时自动执行的方法,而析构方法是在实例被销毁的时候被执行,Python类成员中 ...

  8. Python学习day41-数据库(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  9. python入门灵魂5问--python学习路线,python教程,python学哪些,python怎么学,python学到什么程度

    一.python入门简介 对于刚接触python编程或者想学习python自动化的人来说,基本都会有以下python入门灵魂5问--python学习路线,python教程,python学哪些,pyth ...

随机推荐

  1. [翻译] MAThemeKit

    MAThemeKit https://github.com/mamaral/MAThemeKit MAThemeKit provides iOS developers the ability to c ...

  2. 测试你的 In-app Billing 程序

    测试你的 In-app Billing 程序 为了保证 In-app Billing 可以在你程序中正常使用,你应该在把应用程序发布到Google Play之前进行测试.早期的测试有助于确保用户对于你 ...

  3. 为exchange 2010 owa 添加验证码

    微软给了exchange owa页面加固的方案,如有需要,请查看. https://partnersupport.microsoft.com/zh-hans/par_servplat/forum/pa ...

  4. VS2017配置cuda9.1编译不过问题。

    #if defined(_WIN32) #if _MSC_VER < 1600 || _MSC_VER > 1920 #error -- unsupported Microsoft Vis ...

  5. Python运算符和数据类型

    一.Python的逻辑运算符 1.数字运算符: 运算符 描述 例子 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * ...

  6. POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】

    题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Su ...

  7. 【Vue.js】高仿饿了么外卖App(一)

    1.架构从传统的MVC向REST API+前端MV*迁移 参考链接: http://blog.csdn.net/broadview2006/article/details/8615055 http:/ ...

  8. 9、RabbitMQ-集成Spring

    spring封装RabbitMQ看官网:https://spring.io/projects/spring-amqp#learn 开发时根据官网的介绍进行开发,具体的说明都有具体的声明 1.导入依赖 ...

  9. [转].NET设计模式系列文章

    最初写探索设计模式系列的时候,我只是想把它作为自己学习设计模式的读书笔记来写,可是写到今天,设计模式带给我的震撼,以及许多初学者朋友的热心支持,让我下定决心要把这个系列写完写好,那怕花上再多的时间也无 ...

  10. 使用arcpy替换栅格数据异常

    使用arcpy替换mxd中的栅格图层数据源时,发现导出的图片异常. 修改代码把修改后的mxd保存后发现修改后的数据源不是我传入参数的数据源 比如我设置的参数是 “2019_3_2_2f8091e2d4 ...