How to connect oracle database with python pypyodbc

9.4k views Asked by At

I am trying to connect an oracle database from my python code using pypyodbc:

conn_string = "driver={Oracle in OraClient11g_home1}; server='example.oneco.com:1521'; database='tabto'; uid='myuid'; pwd='mypwd'"
conn = pypyodbc.connect(conn_string)

I got error message:

Error: (u'HY000', u'[HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error\n')

Following is the connection string found in my tnsnames.ora file. Please tell me what is the right way to use pypyodbc.

tabto, tabto.world, tabto.oracleoutsourcing.com, tabto.oneco.com =
  (DESCRIPTION =
    (ADDRESS = 
      (PROTOCOL = TCP)
      (HOST = example.oneco.com)
      (PORT = 1521)
    )
    (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SERVICE_NAME = tabto_dcu)
    )
  )
2

There are 2 answers

1
ju. On BEST ANSWER

The key is link python code to tnsname.ora by defining dbq parameter. So change the code to the following worked.

conn_string = "driver={Oracle in OraClient11g_home1}; dbq='tabto'; uid='myuid'; pwd='mypwd'"
conn = pypyodbc.connect(conn_string)
0
Diogo Sousa On
conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=example.oneco.com:1521;DATABASE=tabto;UID=youruid;PWD=password')

try it without quotes(') of your code in the middle...

check too pyodbc wiki: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows