简体   繁体   English

如何通过 Python 连接到 Oracle ADW 云数据库?

[英]How do you connect to an Oracle ADW cloud database via Python?

I found this website and it suggest that I might be able to connect to my Oracle ADW cloud database using python.我找到了这个网站,它建议我可以使用 python 连接到我的 Oracle ADW 云数据库。 I tried running the below code but keep running into the same error.我尝试运行以下代码,但一直遇到相同的错误。 Anyone have any insight on how to resolve this?任何人都对如何解决这个问题有任何见解? Note: Password is changed for obvious reasons.注意:由于显而易见的原因,密码已更改。

Code in Jupyter Notebooks: Jupyter Notebooks 中的代码:

import cx_Oracle as cx
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
pswd = 'ABC'

#Connect to Autonomous Data Warehouse 
con = cx.connect(user = 'ADMIN', password = pswd)
query = 'SELECT * from TEST123'
data_train = pd.read_sql(query, con=con)

Error:错误:

DatabaseError: Error while trying to retrieve text for error ORA-01804

I get the same error when I run the below code:运行以下代码时出现相同的错误:

...
#Connect to Autonomous Data Warehouse 
con = cx.connect('ADMIN',pswd,"mltest_high")
query = 'SELECT * from TEST123'
data_train = pd.read_sql(query, con=con)

At a guess from the error number and fact the message text wasn't found, cx_Oracle is using Oracle Instant Client libraries, but you have the ORACLE_HOME environment variable set to some other software.根据错误编号和未找到消息文本的事实推测,cx_Oracle 正在使用 Oracle Instant Client 库,但您已将 ORACLE_HOME 环境变量设置为某些其他软件。 If so, unset ORACLE_HOME.如果是,请取消设置 ORACLE_HOME。 Or perhaps you are only using libraries included in a local Oracle DB install and haven't fully set the Oracle environment variables eg haven't set ORACLE_HOME.或者,您可能只使用包含在本地 Oracle DB 安装中的库并且尚未完全设置 Oracle 环境变量,例如尚未设置 ORACLE_HOME。 Or perhaps you might need a more recent version of the Oracle client libraries - get 19c libraries eg Oracle Instant Client.或者,您可能需要更新版本的 Oracle 客户端库 - 获取 19c 库,例如 Oracle Instant Client。 Also check other StackOverflow questions about ORA-1804.另请查看有关 ORA-1804 的其他 StackOverflow 问题。 If you update your question with information about what Oracle software you have installed on the computer running Python, a more detailed answer might be possible.如果您使用有关在运行 Python 的计算机上安装了哪些 Oracle 软件的信息更新您的问题,则可能会得到更详细的答案。

It sounds like you have got the cloud wallet sorted out for connection, but here are references for people coming to this question after reading your heading:听起来您已经为连接整理了云钱包,但以下是阅读您的标题后提出此问题的人的参考:

So this took a lot of education in order to figure out especially when it came to how Oracle wallets work inline with SLQNET.ora and TNS_NAMES.ora file in conjunction with system environmental variables but this website did get my python (in .ipynb) in visual studio code to work in being able to connect with Oracle's cloud ADW system.因此,这需要大量的教育才能弄清楚,尤其是在涉及到 Oracle 钱包如何与 SLQNET.ora 和 TNS_NAMES.ora 文件以及系统环境变量一起工作时,但该网站确实让我的 python(在 .ipynb 中) Visual Studio 代码能够与 Oracle 的云 ADW 系统连接。 It is almost exactly what I did to get it to work on my machine but I didn't do the virtual environment.这几乎正​​是我为让它在我的机器上工作所做的,但我没有做虚拟环境。 I had to figure out a work around with the items stated above but was able to instead use the system link to my wallet for the directory.我不得不想办法解决上述项目,但能够使用指向我钱包的系统链接作为目录。

It is important to know that you need to do these things to get it to work.重要的是要知道您需要执行这些操作才能使其正常工作。 When you download the wallet from ADW, you need to copy the high/medium/low lines from the TNS_NAMES and paste that in your Oracle/network/admin/tns_names.ora file.当您从 ADW 下载钱包时,您需要从 TNS_NAMES 复制高/中/低行并将其粘贴到您的 Oracle/network/admin/tns_names.ora 文件中。 You will also need to take the wallet information and ssl server from the sqlnet.ora file and put it in the sqlnet.ora file in the Oracle/network/admin/ directory as well.您还需要从 sqlnet.ora 文件中获取钱包信息和 ssl 服务器,并将其放入 Oracle/network/admin/ 目录中的 sqlnet.ora 文件中。 If you chose not to use the virtual environment as demonstrated in the post, to get the directory link, for the wallet information line, to work, you'll need to set said directory to the directory of the wallet folder.如果您选择不使用帖子中演示的虚拟环境,要获取目录链接,钱包信息行才能工作,您需要将所述目录设置为钱包文件夹的目录。 I unzipped mine;我解开了我的; unsure if needed or not.不确定是否需要。

Lastly, you will need to set your system environmental variables for TNS_NAMES to wherever your tns_names.ora and sqlnet.ora system files are (not the ones that come in the wallet download folder), likely in Oracle\\network\\admin.最后,您需要将 TNS_NAMES 的系统环境变量设置为 tns_names.ora 和 sqlnet.ora 系统文件所在的任何位置(不是钱包下载文件夹中的那些),可能在 Oracle\\network\\admin 中。

Below is the code that worked for me.下面是对我有用的代码。 I hope this helps someone else and that they don't have to go through the same hoops that I had to in order to figure it out.我希望这可以帮助其他人,并且他们不必像我一样经历同样的过程才能弄清楚。

import cx_Oracle
import os
import pandas as pd

os.environ.get('TNS_ADMIN')
connection = cx_Oracle.connect('<Oracle ADW Username', '<Oracle ADW Password>', '<TNS_NAME entry (high/med/low)>')

cursor = connection.cursor()
rs = cursor.execute("SELECT * FROM TEST123")
df = pd.DataFrame(rs.fetchall())
df

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

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