Python连接oracle

 

import cx_Oracle

conn = cx_Oracle.connect('用户名/密码@IP/ServiceName') #1个参数

conn = cx_Oracle.connect('用户名', '密码', 'IP/ServiceName') #3个参数

打开 D:\app\username\product\11.2.0\client_1\NETWORK\ADMIN路径下的tnsnames.ora

此处的IP为下列连接描述的HOST,ServiceName为SERVICE_NAME 

(DESCRIPTION =
  (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 111.111.111.111)(PORT = 1521))
  )
  (CONNECT_DATA =
    (SERVICE_NAME = orcl)
  )
)

 

刚刚开始连接时,我机器上的oracle是32位客户端连64位服务器端,客户端位于 D:\app\username\product\11.2.0\client_1

尝试连接时,会报错如下:

cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "d:\app\sabre\product\11.2.0\client_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help

按照提示中的链接,找到oracle的64位客户端文件package,下载,放到D:\instantclient_11_2,然后把这个路径添加到系统path中,再次尝试连接,就可以了。

 

posted @ 2017-12-15 19:55  火军刀  阅读(7024)  评论(0编辑  收藏  举报