python使用cx_Oracle连接oracle
1.使用pip命令安装cx_Oracle
$ pip install cx_Oracle
2.安装oracle客户端,并添加到path
下载路径:
http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html
根据对应的系统类型和版本以及oracle版本进行下载,比如64位python的windows平台,oracle版本为11g,
下载instantclient-basic-windows.x64-11.2.0.4.0.zip
http://www.oracle.com/technetwork/topics/winx64soft-089540.html
解压至安装路径,然后将该路径添加至环境变量path后,比如解压在C:\Oracle路径下,那么在path环境变量后面加上C:\Oracle\instantclient_11_2;即可
3.使用示例
import cx_Oracle
conn = cx_Oracle.connect('user/passwd@host/instance')
curs = conn.cursor()
sql = 'select * from product_component_version'
curs.execute(sql)
for result in curs:
print(result)
curs.close()
conn.close()
cx_Oracle连接数据库总结
python中连接oracle数据库使用第三方库文件cx_Oracle时遇到了各种问题,网上查找资料调试了几天才弄好,下面是不断调试后总结的一些经验。
1.oracle客户端(Oracle Instant Client)版本需要和操作系统版本位数相同,同时cx_Oracle官方文档(http://cx-oracle.readthedocs.io/en/latest/installation.html)上有这样一段话
Version 12.2 client libraries can connect to Oracle Database 11.2 or greater. Version 12.1 client libraries can connect to Oracle Database 10.2 or greater. Version 11.2 client libraries can connect to Oracle Database 9.2 or greater.
根据安装的Oracle数据库版本选择Oracle客户端(下载地址 http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html),我安装的oracle数据库是11g版本,这里的客户端库在下载客户端Oracle Instant Client时就包含在内
下载好oracle客户端后,在客户端目录下新建一/network/admin目录,并在该目录下新建tnsnames.ora文件,增加自己的数据库别名配置。
示例如下:

1 MyDB=
2 (DESCRIPTION =
3 (ADDRESS = (PROTOCOL = TCP)(HOST= IP)(PORT = 1521))
4 (CONNECT_DATA =
5 (SERVER = DEDICATED)
6 (SERVICE_NAME = )
7 )
8 )

注意格式要排列好
MyDB为Database名,Host为IP地址, SERVICE_NAME为数据库服务器的实例名。
2.安装的python版本位数也需与操作系统版本位数相同
3.安装的第三方库cx_Oracle需要版本位数和操作系统相同同时,与Oracle数据库对应的版本也应相同,因我安装的数据库是11g,所以下载的是cx_Oracle-5.3-11g.win-amd64-py3.5-2 若安装的数据库是12c则应下载相应版本cx_Oracle(地址 https://pypi.python.org/pypi/cx_Oracle/5.3)
同时可能出现的其他问题在cx_Oracle官方文档中也写出了
1. DPI-1047: Oracle Client library cannot be loaded
Check that Python, cx_Oracle and your Oracle Client libraries are all 64-bit or all 32-bit. The DPI-1047 message will tell you whether the 64-bit or 32-bit Oracle Client is needed for your Python.
检查python,cx_Oracle和Oracle Instant Client版本是否一致,DPI-1047 message会告诉你安装的客户端版本是否正确。
2.On Windows, restart your command prompt and use set PATH to check the environment variable has the correct Oracle Client listed before any other Oracle directories.
记得配置Oracle客户端的环境变量,例如我的配置是 PATH: E:\oracle\instantclient_12_2;
3.On Windows, use the DIR command on the directory set in PATH. Verify that OCI.DLL exists there.
检查oracle客户端(instantclient)目录下存在oci.dll文件
oracle客户端libraries需要正确的Visual Studio版本,具体可见(https://oracle.github.io/odpi/doc/installation.html#windows)中windows目录下
最后一切就绪,程序未出错,但并无输出时感觉内心有些崩溃
刚刚开始连接时,我机器上的oracle是32位客户端连64位服务器端,客户端位于 D:\app\username\product\11.2.0\client_1
尝试连接时,会报错如下:
cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "d:\app\sabre\product\11.2.0\client_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help
按照提示中的链接,找到oracle的64位客户端文件package,下载,放到D:\instantclient_11_2,然后把这个路径添加到系统path中,再次尝试连接,就可以了。

1 import cx_Oracle
2
3 connection = cx_Oracle.Connection("Username/密码@Host:Port/SERVICE_NAME")
4 cursor = connection.cursor()
5
6 try:
7 cursor.execute("select 1 / 0 from dual")
8 except cx_Oracle.DatabaseError as exc:
9 error, = exc.args
10 print("Oracle-Error-Code:", error.code)
11 print("Oracle-Error-Message:", error.message)

最后查看意识到是执行了sql语句,但并未进行输出
在
cursor.execute("select 1 / 0 from dual")
下一行增加
print(cursor.description)
python使用cx_Oracle连接oracle的更多相关文章
- python用cx_Oracle连接oracle
确认版本: oracle版本:64位 python版本:64位 下载cx_Oracle的whl包:64位 安装whl包:pip install wheel cd到下载路径安装cx_Oracle的whl ...
- python使用cx_oracle连接oracle数据库
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html---下载instantclient-basic-linux.x ...
- Python3安装cx_Oracle连接oracle数据库实操总结
弄清版本,最重要!!! 首先安装配置时,必须把握一个点,就是版本一致!包括:系统版本,python版本,oracle客户端的版本,cx_Oracle的版本,然后安装配置就容易了! 如果已经安装Pyth ...
- 在PYTHON中,用cx_Oracle连接ORACLE数据库简单示例
一,在安装的时候,参数有点不一样: python setup.py build install 二,连接数据库,有两种方式,DSN和TNSNAMES方式: #dsn = orcl.makedsn(se ...
- python连接oracle数据库报错"DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "解决方案
操作系统,python3.5, oracle_11, 均为64位:plsql 正常连接. 也顺利安装了cx_oracle 6.3,但是python进行连接的时候就会报错"DatabaseEr ...
- python连接Oracle的方式以及过程中遇到的问题
一.库连接步骤 1.下载cx_Oracle模块 下载步骤 工具 pycharm :File--->右键setting--->找到Project Interpreter -----> ...
- Python使用cx_Oracle模块操作Oracle数据库--通过sql语句和存储操作
https://www.jb51.net/article/125160.htm?utm_medium=referral Python使用cx_Oracle调用Oracle存储过程的方法示例 http ...
- windows10,redhat6.5下python3.5.2使用cx_Oracle链接oracle
0.序言 项目主要使用oracle但是我不太喜欢其他编程语言,加上可能需要用python部署算法包,从oracle表中读出数据,处理完成后在放回oracle中去,所以在windows上就想到先用pyt ...
- Python安装cx_Oracle与操作数据测试小结
这里简单总结一下Python操作Oracle数据库这方面的相关知识.只是简单的整理一下之前的实验和笔记.这里的测试服务器为CentOS Linux release 7.5. 个人实验.测试.采集数据的 ...
随机推荐
- NSData 方法
/****************Immutable Data****************/ @interface NSData : NSObject <NSCopying, NSMutab ...
- 上传控件CSS用图片代替
<style type="text/css"> a.btn {width: 120px;height: 42px;overflow: hidden;display: b ...
- MUI ajax数据请求(list)
服务器返回格式 { "code": "1001", "message": "查询成功", "data" ...
- 关于layer.photos即照片显示的问题。
在layer组件中,照片显示是不常用,今天做了一些不伤了. 在这里写出来,以备后用. 其中注意几个问题, 1.格式问题. 2.路径问题. 不同的layer有不同的格式,查看layerAPI中发现的格式 ...
- 正则表达式—RegEx(RegularExpressio)(三)
今日随笔,继续写一点关于正则表达式的 知识.前两天介绍了正则表达式验证匹配,提取等一些基本的知识,今天继续分享下它的另一个强大的应用:替换(replace). 开始之前,还是要补一下昨天的内容. 在我 ...
- Linux显示不了中文
原文:https://www.cnblogs.com/hooly/p/8615384.html 版权所有:归属原文作者!!! 查看当前系统默认采用的字符集: # locale 在RedHat/C ...
- java的list集合操作List<T>转化List<Long>
java的list集合操作List<T>转化List<Long> package com.google.common.collect; import com.google.co ...
- centos配置Tomcat以指定的身份(非root)运行
本文依赖的环境: 已安装并配置好jdk和tomcat环境 已安装并配置好gcc.make等编译工具 1.编译安装守护程序 cd /usr/local/tomcat7/bin/ tar vzxf c ...
- 微信小程序 --- 页面渲染
page.wxml文件 <view>{{text}}</view> page.js 文件: //获取应用实例 const app = getApp() Page({ data: ...
- IIS Admin Service 服务由于下列服务特定错误而终止: 无效签名。
于是查看系统日志: 具体信息如下:日志名称: System来源: Service Control Manager日期: 2015/11/2 ...