通过pyodbc将Python连接到Oracle数据库

hmae6n7t  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(195)

我正在尝试使用pyodbc连接到oracle数据库。下面是我开发的Python代码:

import pyodbc
conn=pyodbc.connect('DRIVER={Oracle in OraClient19Home1};Host=xxxxxx.yyy.com;Port=1521;Service Name=myservicename;User ID=myuserid;Password=mypassword')

遗憾的是,上述代码不起作用,因为它返回以下错误:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\balestram\OneDrive - World Health Organization\Documents\Data Analytics\Python Training> & C:/Users/balestram/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/balestram/OneDrive - World Health Organization/Documents/Data Analytics/Python Training/python"
Traceback (most recent call last):
  File "c:\Users\xxxxxxxxx\OneDrive - xxxxxxxxxxxxxxxxxxxxx\Documents\Data Analytics\Python Training\python", line 2, in <module>
    conn=pyodbc.connect('DRIVER={Oracle in OraClient19Home1};Host=xxxxxx.yyy.com;Port=1521;Service Name=myservicename;User ID=myuserid;Password=mypassword')
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyodbc.Error: ('HY000', '[HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error\n (12560) (SQLDriverConnect); [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error\n (12560)')
PS C:\Users\xxxxxxxxxx\OneDrive - xxxxxxxxxxxxxxxxxxxxx\Documents\Data Analytics\Python Training>

我必须补充一点,如果我使用Ms Access和/或Ms Power BI连接到Oracle数据库,上述连接参数工作正常。
感谢任何能让我走上正轨的人。

qojgxg4l

qojgxg4l1#

请尝试以下方法,看看是否有效。Taken from the pyodbc documentation

pyodbc.connect('DRIVER={Oracle in OraClient19Home1};DBQ=xxxxxx.yyy.com:1521/myservicename;
 UID=myuserid;PWD=mypassword'))"

相关问题