Python 你如何使用pyodbc连接到oracle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27363938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:43:05  来源:igfitidea点击:

how do you connect to oracle using pyodbc

pythonoracle

提问by user1471980

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

我正在尝试使用 pyodbc 连接到 Oracle 数据库,但出现错误。示例包括 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)')

采纳答案by Valeriy Gaydar

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

回答by Andrei Sura

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

    [FreeTDS]

    Description=FreeTDS Driver for Linux & MSSQL on MacOS

    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. Edit ~/Library/ODBC/odbc.ini

    [sql_server]

    Driver=FreeTDS

    Server=put_ip_here

    Port=1433

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

    [免费TDS]

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

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

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

    文件使用率=1

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

    [sql_server]

    驱动程序=FreeTDS

    服务器=put_ip_here

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

回答by aditya khatri

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-odbc-12.2.0.1.0-2.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

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

回答by Alexis.Rolland

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

pyodbc 维护者在记录如何安装 Oracle ODBC 驱动程序以及如何连接到数据库方面做得非常出色。它对我有用https: //github.com/mkleehammer/pyodbc/wiki/Connecting-to-Oracle-from-RHEL-or-Centos