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文件,增加自己的数据库别名配置。
示例如下:

 MyDB=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST= IP)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = )
)
)

注意格式要排列好
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文件

4.On Windows, check that the correct Windows Redistributables have been installed.
oracle客户端libraries需要正确的Visual Studio版本,具体可见(https://oracle.github.io/odpi/doc/installation.html#windows)中windows目录下

On Linux, check the LD_LIBRARY_PATH environment variable contains the Oracle Client library directory.
On macOS, make sure Oracle Instant Client is in ~/lib or /usr/local/lib and that you are not using the bundled Python (use Homebrew or Python.org instead).

最后一切就绪,程序未出错,但并无输出时感觉内心有些崩溃

 import cx_Oracle

 connection = cx_Oracle.Connection("Username/密码@Host:Port/SERVICE_NAME")
cursor = connection.cursor() try:
  cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError as exc:
  error, = exc.args
print("Oracle-Error-Code:", error.code)
print("Oracle-Error-Message:", error.message)

最后查看意识到是执行了sql语句,但并未进行输出

cursor.execute("select 1 / 0 from dual")

下一行增加
 print(cursor.description) 
便可以看见查找到的数据库中的内容

cx_Oracle连接数据库总结的更多相关文章

  1. python使用cx_Oracle连接oracle

    1.使用pip命令安装cx_Oracle $ pip install cx_Oracle 2.安装oracle客户端,并添加到path 下载路径: http://www.oracle.com/tech ...

  2. Python:操作数据库

    (一)      前言 本文说明如何连接Oracle.MySQL.sqlserver,以及执行sql.获取查询结果等. (二)      DB-API      DB-API阐明一系列所需对象和数据库 ...

  3. python调用oracle存储过程

    oracle 存储过程 python调用oracle存储过程 -- 通过cx_Oracle连接 import cx_Oracle # 连接数据库 orcl_engine = 'scott/s123@x ...

  4. Python3 连接各类数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口.它定义了一系列必须的对象和数据库存取方式, 以便为各种各样的底层数据库系统和多 ...

  5. cx_Oracle模块详解

    1.安装cx_Oracle模块 1-1.环境准备: 1-1-1.oracle client最小安装 instantclient-sqlplus-linux.x64-11.2.0.4.0 instant ...

  6. windows10,redhat6.5下python3.5.2使用cx_Oracle链接oracle

    0.序言 项目主要使用oracle但是我不太喜欢其他编程语言,加上可能需要用python部署算法包,从oracle表中读出数据,处理完成后在放回oracle中去,所以在windows上就想到先用pyt ...

  7. Python中使用cx_Oracle调用Oracle存储过程

    import cx_Oracle as cx import datetime def execute_sql(): # 声明变量 date_time = datetime.datetime.now() ...

  8. 安装cx_Oracle 6

    首先声明,本文是在Linux 环境下进行安装.不涉及Windows 版安装. 一. 了解cx_Oracle 安装要求 要python 通过cx_Oracle 6 操作Oracle 数据库,以下几个条件 ...

  9. python cx_Oracle模块的安装和使用

      $wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip 3 ...

随机推荐

  1. keystore是个嘛东西

    不管是QQ,还是微信,还是支付,涉及到第三方的都的用这个玩意,有时候找不对很坑的 首先我们要区分jks, app,keystore(新建keystoer的文件new就可以了)再进行下一步操作 Ecli ...

  2. kotlin 遇到的问题

    转载请表明 https://i.cnblogs.com/EditPosts.aspx?opt=1 从5月18号goole正式公布用kotlin做为android的新语言,做为android也很庆幸可以 ...

  3. python真值表

    author:headsen chen  date :2018-06-01  10:53:39 notice:not allowed to copy or you will count law que ...

  4. C 语言开发初涉-01 简单学习

    尝试用C语言写windows程序: 简单的计算器 1.0版,仅用来熟悉C 开发windows的一些语法和语句用法 #include "stdafx.h" #include < ...

  5. 160304-01、mysql数据库插入速度和读取速度的调整记录

    需求:由于项目变态,需要在一个比较短时间段急剧增加数据库记录(两三天内,由于0增加至5亿).在整个过程调优过程非常艰辛 思路: (1)提高数据库插入性能中心思想:尽量将数据一次性写入到Data Fil ...

  6. 后台程序在向tty/串口写数据的时候stop了

    当后台程序向tty/串口写数据的时候stop了. STOPPED(SIGTTOU) .... SIGTTOU:代表的是后台程序向 controlling terminal写数据. 解决办法:暂时在程序 ...

  7. 【Python算法】递归与递归式

    该树结构显示了从1(根节点)到n(n个叶节点)的整个倍增过程.节点下的标签表示从n减半到1的过程. 当我们处理递归的时候,这些级数代表了问题实例的数量以及对一系列递归调用来说处理的相关工作量. 当我们 ...

  8. Webpack4.x 入门

    概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...

  9. explain(desc)命令的使用

    获取 type:查询类型 1.可以判断出,全表扫描还是索引扫描(ALL就是全表扫描,其他就是索引扫描) 2.对于索引扫描来讲,可以西划分,可以判断是哪一种扫描 type的具体类型介绍: ALL:全表扫 ...

  10. 【我的Android进阶之旅】Jenkins挂载slave节点,增强分布式编译的效率

    由于公司的Jenkins任务越来越多,而且所有的Android Jenkins任务都在同一台服务器上进行编译,而且该服务器配置Jenkins任务最多3个任务同时运行,所以有时候大家一起编译的时候,只能 ...