天天看点

Windows下Python3连接oracle环境搭建

1、安装Python3

2、安装cx_Oracle(Python的扩展包),用来访问Oracle数据库。

下载地址:https://pypi.org/simple/cx-oracle/

下载的是whl:

cx_Oracle-7.2.3-cp36-cp36m-win_amd64.whl

cp36:代表Python3.6
           
Windows下Python3连接oracle环境搭建
Windows下Python3连接oracle环境搭建
Windows下Python3连接oracle环境搭建

3、安装oracle客服端(这个是Python扩展包所需要的)

下载地址:http://jvniu.jb51.net:81/201708/tools/instantclientx64_jb51.rar

如果windows电脑上还没有安装过客户端,将下载的文件解压之后添加到Path环境变量:

D:\oracleClient\instantclient_11_2
           

如果已经安装过客户端则放到如下位置,再将此位置添加到Path环境变量:

Windows下Python3连接oracle环境搭建

4、代码测试

import cx_Oracle,os
def main():
    os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
    host=""
    port=""
    sid=""
    user=""
    pwd=""
    sn=cx_Oracle.makedsn(host,port,sid)
    conn=cx_Oracle.connect(user,pwd,sn)
    c=conn.cursor()
    c.execute("select * from dual")
    # row=x.fetchone()
    result=c.fetchall()
    conn.commit()
    for ele in result:
        #print(ele[1])
        print(ele)
    c.close()
    conn.close()

main()