简体   繁体   English

你如何使用pyodbc连接到oracle

[英]how do you connect to oracle using pyodbc

I am trying to connect to Oracle db using pyodbc, getting errors.我正在尝试使用 pyodbc 连接到 Oracle 数据库,但出现错误。 The examples include ms sql server driver:示例包括 ms sql server 驱动程序:

in my /etc/unixODBC/odbc.ini, I have this entry:在我的 /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')

I get this error:我收到此错误:

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

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.您可以使用 cx_Oracle 库,在没有 pyodbc 的情况下非常简单地连接到 Oracle 数据库。 Check out the installation instructions below:查看下面的安装说明:

https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html 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()

pyodbc maintainer did an excellent job at documenting how to install Oracle ODBC driver and then how to connect to the database. pyodbc 维护者在记录如何安装 Oracle ODBC 驱动程序以及如何连接到数据库方面做得非常出色。 It worked for me: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Oracle-from-RHEL-or-Centos它对我有用https : //github.com/mkleehammer/pyodbc/wiki/Connecting-to-Oracle-from-RHEL-or-Centos

The code below is for mssql on Mac OS and it assumes that you installed unixODBC using:下面的代码适用于 Mac OS 上的 mssql,它假定您使用以下命令安装了 unixODBC:

$ brew install freetds --with-unixodbc

and that you have edited the two odbc config files:并且您已经编辑了两个 odbc 配置文件:

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

    [FreeTDS] [免费TDS]

    Description=FreeTDS Driver for Linux & MSSQL on MacOS说明=适用于 MacOS 上的 Linux 和 MSSQL 的 FreeTDS 驱动程序

    Driver=/usr/local/Cellar/freetds/0.95.80/lib/libtdsodbc.0.so驱动程序=/usr/local/Cellar/freetds/0.95.80/lib/libtdsodbc.0.so

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

    FileUsage=1文件使用率=1

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

    [sql_server] [sql_server]

    Driver=FreeTDS驱动程序=FreeTDS

    Server=put_ip_here服务器=put_ip_here

    Port=1433端口=1433

== Code: ==代码:

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

The reply has been late but can be useful for future reader.回复晚了,但对未来的读者有用。

Install:安装:

  • oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm 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 oracle-instantclient12.2-odbc-12.2.0.1.0-2.x86_64.rpm

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

sudo rpm -Uvh oracle-instantclient12.2-*

set ORACLE_HOME and LD_LIBRARY_PATH设置 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

In /etc/odbcinst.ini set:在 /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

In Python shell:在 Python 外壳中:

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

Hopefully it helps someone.希望它可以帮助某人。

PS: You can also set the driver in /etc/ld.so.conf as PS:你也可以在/etc/ld.so.conf中设置驱动为

/usr/lib/oracle/12.2/client64/lib

Run: ldconfig运行:ldconfig

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阅读一些文档;) https://sites.google.com/site/bcgeopython/examples/getting-the-pyodbc-module

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM