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:Oracleinstantclient_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()