如何使用pyodbc与Oracle建立连接

7
我将尝试使用pyodbc连接Oracle数据库,但出现了错误。示例包括ms sql server driver:
在我的/etc/unixODBC/odbc.ini中,我有以下条目:
[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')

我遇到了这个错误:

pyodbc.Error: ('IM012', '[IM012] [unixODBC][Driver Manager]DRIVER keyword syntax error (0) (SQLDriverConnect)')

如果你解决了这个问题,能否帮我解决一下我的问题? - shivshankar
这对我有用 https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Oracle-from-RHEL-or-Centos - Alexis.Rolland
5个回答

7

我来这里寻找一个问题的答案,但是我在别处找到了一个更一般的问题的答案,我想分享一下。你可以使用cx_Oracle库非常简单地连接到Oracle数据库,而无需使用pyodbc。请查看下面的安装说明:

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

以下是初始代码:

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()

1
cx_Oracle 的最新版本已更名为 python-oracledb,请查看发布公告。用法与上文相同。安装说明可参见此处。与 pyodbc 相比,python-oracledb 驱动具有与 pyodbc 相同的标准 DB API,并且拥有许多针对 Oracle 的扩展,速度更快,并且无需安装任何额外的库(即不需要安装 Oracle Instant Client)。 - Christopher Jones

2

0
以下代码适用于在Mac OS上使用mssql,并假定您已经安装了unixODBC:
$ brew install freetds --with-unixodbc

请确认您已编辑了两个ODBC配置文件:

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

    [FreeTDS]

    Description=适用于Linux和MacOS上的MSSQL的FreeTDS驱动程序

    Driver=/usr/local/Cellar/freetds/0.95.80/lib/libtdsodbc.0.so

    Setup=/usr/local/Cellar/freetds/0.95.80/lib/libtdsodbc.0.so

    FileUsage=1

  2. 编辑~/Library/ODBC/odbc.ini

    [sql_server]

    Driver=FreeTDS

    Server=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")
    ...

MSSQL的示例没有解决Oracle连接问题。 - Ben M
这个问题是针对Oracle的,而FreeTDS驱动程序是针对MsSQL Server的。 - Alexis.Rolland

0
回复有些晚了,但对未来的读者可能会有用。
安装:
  • oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
  • oracle-instantclient12.2-odbc-12.2.0.1.0-2.x86_64.rpm

From: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

sudo rpm -Uvh oracle-instantclient12.2-*

设置 ORACLE_HOME 和 LD_LIBRARY_PATH

export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib

在/etc/odbcinst.ini中设置:
[Oracle_test]
Description=Oracle ODBC driver for Oracle 12c
Driver=/usr/lib/oracle/12.2/client64/lib/libsqora.so.12.1
FileUsage=1
Driver Logging=7
UsageCount=1

在Python shell中:

>>> import pyodbc
>>> conn = pyodbc.connect('DRIVER={Oracle_test};Host=1.1.1.1;Port=1521;Service Name=orcl.local;User ID=test1;Password=test1')
>>> print(conn)
<pyodbc.Connection object at 0x7f6acb2c4c00> 

希望能对某人有所帮助。
附注:你也可以将驱动程序设置在 /etc/ld.so.conf 中。
/usr/lib/oracle/12.2/client64/lib

运行:ldconfig


-5

@ValeiryG,我看了一下那个链接,它是SQL驱动程序,而我在一个Linux服务器上。不过我还是尝试了一下: pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)') - user1471980

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接