python使用cx-oracle连接oracle

5 篇文章 0 订阅

今天尝试在flask使用orcale数据库,需要用cx-oracle

pip install cx-oracle

结果报错

the Oracle Client library version is unsupported

以为是cx-oracle版本的问题,下调cx-oracle到6.0

pip uninstall cx-oracle
pip install cx-oracle==6.0

提示

Oracle Client library is at version 0.0 but version 11.2 
or higher is needed

提示这个是因为python和oracle客户端版本不对应。查了下,原来venv中python是32位的,在oracle官网下了个最新的32位instanceclient,安装设置环境变量。再次运行

ORA-12705: Cannot access NLS data files or invalid 
environment specified

每次错误都不一样。
这个是因为NLSlanguage不同造成的。使用plsql查询NLS参数

SELECT USERENV ('language') FROM DUAL

得到 SIMPLIFIED CHINESE_CHINA.ZHS16GBK
然后在注册表

\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ORACLE

这里找到 NLS_LANG ,数值是NA改成上边的,再次运行。
得到想要的结果。最后是py文件的代码。

import cx_Oracle

connection = cx_Oracle.Connection("userid/password@host:port/service_name")
cursor = connection.cursor()

try:
    cursor.execute("select * from tablename")
except Exception as err:
    print("Whoops!")
    print(err)
# print(cursor.description)
for row in cursor.execute("select * from tablename"):
    print(row)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值