Python pyodbc - Error - [unixODBC][Oracle][ODBC][Ora]ORA-12162: TNS:net service name is incorrectly specified
One may encounter below errors while connecting to oracle, using pyodbc, using python 3
- [unixODBC][Driver Manager]Can't open lib 'Oracle ODBC driver for Oracle 19' : file not found (0) (SQLDriverConnect)
- [unixODBC][Oracle][ODBC][Ora]ORA-12162: TNS:net service name is incorrectly specified\n (12162) (SQLDriverConnect)
- RuntimeError: Unable to set SQL_ATTR_CONNECTION_POOLING attribute
The solution to fix above errors is to -
- Make following entry in /etc/odbcinst.ini
Description=Oracle ODBC driver for Oracle 19
Driver=$ORACLE_HOME/lib/libsqora.so.19.1
FileUsage=1
Driver Logging=7
UsageCount=1
- Don't use following connect string -
import pyodbc
myhost='<>'
myservicename='<>'
myuserid='<>'
mypassword='<>'
- Instead use following connection String -
- To execute SQL -
cursor = cnxn.cursor()
cursor.execute("select 1 from dual;")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
Comments
Post a Comment