Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you connect to oracle using pyodbc

Tags:

python

oracle

I am trying to connect to Oracle db using pyodbc, getting errors. The examples include ms sql server driver:

in my /etc/unixODBC/odbc.ini, I have this entry:

[test_con]
Driver=Oracle
Description=data repository db
Trace=Yes
ServerName=//db1.example.com:1521/db2_svc1


import pyodbc
cnxn=pyodbc.connect('DSN=test_con, UID=user_id, PWD=passwd123')

I get this error:

pyodbc.Error: ('IM012', '[IM012] [unixODBC][Driver Manager]DRIVER keyword syntax error (0) (SQLDriverConnect)')
like image 837
user1471980 Avatar asked Dec 08 '14 17:12

user1471980


People also ask

Can we connect Oracle with Python?

By this command, you can install cx-Oracle package but it is required to install Oracle database first on your PC. connect(): Now Establish a connection between the Python program and Oracle database by using connect() function.

Can we use ODBC to connect to Oracle Database?

The ODBC driver has Oracle's standard client-server version interoperability, see Support Doc ID 207303.1. For example Instant Client ODBC 19c can connect to Oracle Database 11.2 or later.


2 Answers

I came here looking for an answer to this question, but instead found an answer elsewhere to a more general question that I'd like to share. You can very simply connect to an Oracle db without pyodbc, using the cx_Oracle library. Check out the installation instructions below:

https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html

Starter code below:

cx_Oracle.init_oracle_client(lib_dir=r"C:\oracle\instantclient_19_10")

connection = cx_Oracle.connect(
user="username",
password="password",
dsn="address")

cursor = connection.cursor()
like image 78
Evan Larson Avatar answered Oct 06 '22 10:10

Evan Larson


pyodbc maintainer did an excellent job at documenting how to install Oracle ODBC driver and then how to connect to the database. It worked for me: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Oracle-from-RHEL-or-Centos

like image 25
Alexis.Rolland Avatar answered Oct 06 '22 10:10

Alexis.Rolland