您的位置:首页 > 数据库 > Oracle

windows下Python连接Oracle

2016-08-31 16:36 537 查看
安装出错:

distutils.errors.DistutilsSetupError: cannot locate Oracle include files in

或者运行出错:

cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

均可参考该文章

环境:

windows10(同样适用于windows7,8),python2.7

1.下载cx_Oracle

在windows下不要使用easy_install或者pip,因为这样安装不会同步环境,并报错:

distutils.errors.DistutilsSetupError: cannot locate Oracle include files in...

因此下载.exe文件安装
https://pypi.python.org/pypi/cx_Oracle 下载cx_Oracle-5.2.1-12c.win-amd64-py2.7.exe (md5)

2.下载Oracle Instant Client

去http://www.oracle.com/technetwork/topics/winx64soft-089540.html下载 instantclient-basic-windows.x64-12.1.0.2.0.zip 

下载后解压到某一路径,并将该路径添加到系统的环境变量,命名为ORACLE_HOME,将ORACLE_HOME添加到path下,否则会报错

cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

3.Python下测试

import cx_Oracle

dsn=cx_Oracle.makedsn("192.168.0.233", 1521, "TONGJI")
conn=cx_Oracle.connect("SAS","Tongji123",dsn)
curs=conn.cursor()
sql='select * from CASE_INFO where rownum<10'
rr=curs.execute (sql)
row=curs.fetchone()
print row[0]
curs.close()
conn.close()


输出正确

注意:不需要再本机上安装Oracle,也不需要新建NETWORK/ADMIN目录添加tnsnames.ora文件,更不要把oci.dll移到site-packages
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python oracle