How do you connect to oracle using pyodbc

I am trying to connect to Oracle db using pyodbc, getting errors. 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)')
+4
source share
2 answers

Try something like:

import pyodbc
connectString = 'Driver={Microdsoft ODBC for Oracle};Server=<host>:<port>/<db>.<host>;uid= <username>;pwd=<password>'
cnxn = pyodbc.connect(connectString)

Read some docs;) https://sites.google.com/site/bcgeopython/examples/getting-the-pyodbc-module

-3
source

The following is the code for mssql on Mac OS and it is assumed that you installed unixODBC using:

$ brew install freetds --with-unixodbc

and that you edited two odbc configuration files:

  • /usr/local/Cellar/unixodbc/2.3.2_1/etc/odbcinst.ini

    [FreeTDS]

    = FreeTDS Linux MSSQL MacOS

    =/USR///FreeTDS/0.95.80/Library/libtdsodbc.0.so

    =/USR///FreeTDS/0.95.80/Library/libtdsodbc.0.so

    FileUsage = 1

  • ~/Library/ODBC/odbc.ini

    [sql_server]

    Driver = FreeTDS

    = put_ip_here

    Port = 1433

== :

import pyodbc

connection_string = "Driver={{FreeTDS}};Server={};"\
"Database={};UID={};PWD={};"\
.format(db_host, db_name, db_user, db_password)

with pyodbc.connect(connection_string) as db:
    cursor = db.cursor()

    cursor.execute("SELECT count(*) FROM aTable")
    ...
0

All Articles