python连接oracle教程

用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对。

编写的python脚本中需要加入如下几句:

import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

这样可以保证select出来的中文显示没有问题。

要能够正常的insert和update中文,还需要指定python源文件的字符集密码和oracle一致。

python连接oracle数据库报错"DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "解决方案

2018年05月18日 10:29:28 guimaxingmc 阅读数:6717 标签: pythonoracle 更多

个人分类: 数据库环境配置

操作系统,python3.5, oracle_11, 均为64位;plsql 正常连接。

也顺利安装了cx_oracle 6.3,但是python进行连接的时候就会报错"DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: " 。

原因:

    instantclient版本为32位,需更换成64位。

解决方案:

一、已安装oracle客户端

1. 重新下载 instantclient 64位, 下载链接:http://jvniu.jb51.net:81/201708/tools/instantclientx64_jb51.rar

  下载完成后,解压得到 文件夹
python连接oracle教程_第1张图片
image

2 将整个文件夹移动到oracle安装目录,client子文件夹内

python连接oracle教程_第2张图片
image

3. 添加环境变量(下图为win10系统)

python连接oracle教程_第3张图片
image

4. 重启python, 成功连接oracle。

二、未安装oracle客户端(需要连接服务器数据库的情况)

1、创建文件路径:

D:\Oracle11g\product\11.2.0

2、下载 instantclient 64位 放置到 1 创建的路径下

3、将文件中后缀为 dll 的文件复制到 anaconda 安装位置

python连接oracle教程_第4张图片
image
python连接oracle教程_第5张图片
image

3. 、添加环境变量(见第一种情况)

4、重启python

脚本:
from future import print_function

import cx_Oracle

Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
connection = cx_Oracle.connect("hr", "welcome", "localhost/orclpdb")

cursor = connection.cursor()
cursor.execute("""
SELECT first_name, last_name
FROM employees
WHERE department_id = :did AND employee_id > :eid""",
did = 50,
eid = 190)
for fname, lname in cursor:
print("Values:", fname, lname)
cursor.close()
connection.close()

你可能感兴趣的:(python连接oracle教程)