下载cx_Oracle模块模块:

https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads

这里下载的是源码进行安装

[root@oracle oracle]# tar xf cx_Oracle-5.2.1.tar.gz
[root@oracle oracle]# cd cx_Oracle-5.2.1

[root@oracle cx_Oracle-5.2.1]# python setup.py build
Traceback (most recent call last):
File "setup.py", line 170, in <module>
raise DistutilsSetupError("cannot locate an Oracle software " \
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

报错解决办法:在root用户的.bash_profile文件中添加oracle_home的环境变量

export ORACLE_HOME=/u01/app/product/11.2.0/dbhome_1
PATH=${ORACLE_HOME}/bin:$PATH:$HOME/bin

[root@oracle cx_Oracle-5.2.1]# source /root/.bash_profile
[root@oracle cx_Oracle-5.2.1]# echo ${ORACLE_HOME}
/u01/app/product/11.2.0/dbhome_1

然后继续build:

[root@oracle cx_Oracle-5.2.1]# python setup.py build
running build
running build_ext

后面输出信息省略

[root@oracle cx_Oracle-5.2.1]# python setup.py install
running install
running bdist_egg

后面输出信息省略

按照完成之后,进行验证模块:

>>> import cx_Oracle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

解决办法:在root用户的.bash_profile文件中添加LD_LIBRARY_PATH的环境变量

LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/local/lib
export LD_LIBRARY_PATH

[root@oracle cx_Oracle-5.2.1]# source /root/.bash_profile

[root@oracle cx_Oracle-5.2.1]# python
Python 3.6.1 (default, Jul 13 2017, 14:31:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle

#!/usr/bin/python
#coding=utf8 # import module
import cx_Oracle as oracle # connect oracle database
db = oracle.connect('scott/redhat@192.168.223.138:1521/oracle.test') # create cursor
cursor = db.cursor() # execute sql
cursor.execute('select sysdate from dual') # fetch data
data = cursor.fetchone() print('Database time:%s' % data) # close cursor and oracle
cursor.close()
db.close()

[oracle@oracle scripts]$ python connectoracle.py
Database time:2017-08-04 10:20:39

#!/usr/bin/python
#coding=utf8 import cx_Oracle as oracle def oraclesql(cursor):
fp = open(r'/home/oracle/scripts/tablespace.sql')
fp_sql = fp.read()
cursor.execute(fp_sql)
data = cursor.fetchall()
return data if __name__ == '__main__':
ipaddr = "192.168.223.138"
username = "system"
password = "redhat"
oracle_port = "1521"
oracle_service = "oracle.test"
try:
db = oracle.connect(username+"/"+password+"@"+ipaddr+":"+oracle_port+"/"+oracle_service)
# 将异常捕捉,然后e就是抛异常的具体内容
except Exception as e:
print(e)
else:
cursor = db.cursor()
data = oraclesql(cursor)
for i in data:
print(i)
cursor.close()
db.close()

  

python3.6连接oracle数据库的更多相关文章

  1. Python3安装cx_Oracle连接oracle数据库实操总结

    弄清版本,最重要!!! 首先安装配置时,必须把握一个点,就是版本一致!包括:系统版本,python版本,oracle客户端的版本,cx_Oracle的版本,然后安装配置就容易了! 如果已经安装Pyth ...

  2. python3.5连接oracle数据及数据查询

    今天心血来潮研究下用python连接oracle数据库,看了一下demo,本以为很简单,从操作到成功还是有点坎坷,这里分享给大家,希望为后面学习的童鞋铺路. 一.首先按照cx_Oracle 二:在py ...

  3. Python3 连接 Oracle 数据库

    Python3 连接 Oracle 数据库 需要导出一些稍微复杂的数据,用Python处理很方便 环境 Win10 Python 3.7.0 Oracle 11g 安装依赖 安装 cx_Oracle ...

  4. java连接Oracle数据库

    Oracle数据库先创建一个表和添加一些数据 1.先在Oracle数据库中创建一个student表: create table student ( id ) not null primary key, ...

  5. 记录排查解决Hubble.Net连接Oracle数据库建立镜像库数据丢失的问题

    起因 前几天在弄Hubble连接Oracle数据库,然后在mongodb中建立一个镜像数据库; 发现一个问题,原本数据是11W,但是镜像库中只有6w多条; 刚开始以为是没运行好,又rebuild了一下 ...

  6. NodeJs连接Oracle数据库

    nodejs连接oracle数据库,各个平台的官方详情文档:https://github.com/oracle/node-oracledb/blob/master/INSTALL.md 我的nodej ...

  7. jdbc连接oracle数据库

    /*** 通过改变配置文件来连接不同数据库*/package com.xykj.jdbc; import static org.junit.Assert.*; import java.io.Input ...

  8. 用VS连接oracle数据库时ORA-12504错误

    在用VS2008连接oracle数据库时,可能会出现: ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME 只需在web.config文件Data ...

  9. ASP.NET连接Oracle数据库的步骤详解(转)

    ASP.NET连接Oracle数据库的步骤详解   本文我们主要介绍了ASP.NET连接Oracle数据库的步骤及每个步骤需要进行的设置,希望能够对您有所帮助.   在用ASP.NET开发应用程序时, ...

随机推荐

  1. C# 妈妈再打我一下生成器

    设计背景 网上很火的一个"妈妈再打我一下"的漫画图片,给了网友无限的想象发挥空间,此小程序可以给图片添加配文的形式,快速生成图片 设计思路 GDI+ 绘图技术,在图片基础上添加文字 ...

  2. 获取移动端 touchend 事件中真正触摸点下方的元素

    移动端的touchstart, touchmove, touchend三个事件,拖动元素结束时,获取到了touchend事件, 但是event.touches[0].target所指向的元素却是tou ...

  3. kubeadm安装部署kubernetes 1.11.3(单主节点)

    由于此处docker代理无法使用,因此,请各位设置有效代理进行部署,勿使用文档中的docker代理.整体部署步骤不用改动.谢谢各位支持. 1.部署背景 操作系统版本:CentOS Linux rele ...

  4. 牛客网暑期ACM多校训练营(第一场)J Different Integers

    链接:https://www.nowcoder.com/acm/contest/139/J 题意: 给你[l,r]问[1,l],[r,n]中有多少个不同的数. 思路: 可以参考上一篇博客:https: ...

  5. oracle12c ORA-28040: No matching authentication protocol

    出错原因:11G客户端连12C数据库服务端会报这个错 解决方案一:CSDN优质解决方案,大家都说可以,然而我这边操作了不行 转自13楼:http://bbs.csdn.net/topics/39066 ...

  6. dp乱写2:论dp在不在dp中(但在dp范畴)内的应用

    最近正儿八经的学习了dp,有一些题目非常明显看出来就是dp了比如说:过河卒.方格取数.导弹拦截.加分二叉树.炮兵阵地更加明显的还有:采药.装箱问题.过河.金明的预算方案.今天来谈谈dp的dp在不在dp ...

  7. linux下编译make文件报错“/bin/bash^M: 坏的解释器,使用grep快速定位代码位置

    一.linux下编译make文件报错“/bin/bash^M: 坏的解释器 参考文章:http://blog.csdn.net/liuqiyao_01/article/details/41542101 ...

  8. 如何用javascript获取和设置css3属性

    ==================获取======================== 我想到的第一个思路 var test = document.getElementById('test'); c ...

  9. Java SSM框架之MyBatis3(五)MyBatis之ResultMap详解

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  10. SQL Server 的字段不为NULL时唯一

    CREATE UNIQUE NONCLUSTERED INDEX 索引名称ON 表名(字段) WHERE 字段 is not null SQL Server 2008+ 支持