[英]Connect to Oracle database using pyodbc
I would like to connect to an Oracle database with python through pyodbc.我想通过pyodbc用python连接到Oracle数据库。 I have installed oracle driver and I tried the following script:
我已经安装了 oracle 驱动程序并尝试了以下脚本:
import pyodbc
connectString = """
DRIVER={Oracle in OraClient12Home1};
SERVER=some_oracle_db.com:1521;
SID=oracle_test;
UID=user_name;
PWD=user_pass
"""
cnxn = pyodbc.connect(connectString)
I got the following error message:我收到以下错误消息:
cnxn = pyodbc.connect(connectString)
Error: ('HY000', '[HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error\n (12560) (SQLDriverConnect)')
What's wrong here?这里有什么问题?
Why keyword DBQ
works and SID
/ Service Name
does not, see the section 21.4.1 Format of the Connection String
in Oracle 12c documentation.为什么关键字
DBQ
有效而SID
/ Service Name
无效,请参阅 Oracle 12c 文档中21.4.1 Format of the Connection String
部分。 https://docs.oracle.com/database/121/ADFNS/adfns_odbc.htm#ADFNS1183/ https://docs.oracle.com/database/121/ADFNS/adfns_odbc.htm#ADFNS1183/
or google keywords for odbc oracle 12c
或
keywords for odbc oracle 12c
Looks Like your missing a PORT看起来你缺少一个端口
Try this way试试这个方法
NOTE: Depending on your Server the syntax can be different this will work for Windows without DSN using an SQL Server Driver.注意:根据您的服务器,语法可能会有所不同,这适用于没有使用 SQL Server 驱动程序的 DSN 的 Windows。
connectString = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')
This is the connection, you still need a cursor and to use execute along with an SQL Statement..这是连接,您仍然需要一个游标并使用执行和 SQL 语句..
您必须在数据库服务器正在运行的连接字符串中指定服务器或主机名(或 IP 地址)。
So close...很近...
connectString = """
DRIVER={Oracle in OraClient12Home1};
SERVER=some_oracle_db.com:1521;
DBQ=oracle_test;
Uid=user_name;
Pwd=user_name
"""
I replaced SID with DBQ我用 DBQ 替换了 SID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.