使用 cx_Oracle + Python 从另一个所有者访问表

Accessing tables from another owner using cx_Oracle + Python

我在一家公司工作,他们在 Oracle 数据库中以不同的所有者组织他们的项目。 使用Python + cx_Oracle,可以列出根数据库中的所有表,如下例所示:

我什至在官方 cx_Oracle 文档中找不到任何关于如何访问其他所有者的表的说明。在 Oracle SQL Developer 中,我可以 运行 查询,但是当我尝试 运行 在 Python 中的相同查询时却不能,如下所示:

PS: ORA-00942: a tabela ou view não existe -> ORA-00942: table or view does not exist

原代码:

import cx_Oracle

try:
    connection = cx_Oracle.connect(
        user='SYSTEM',
        password='123456',
        dsn='localhost:1521/XEPDB1',
        encoding='UTF-8'
    )
    print(connection.version)

    c = connection.cursor()
    rows = c.execute("select con_id,name,open_mode,restricted,open_time from v$pdbs").fetchall()
    for row in rows:
        print(row)

except Exception as ex:
    print(ex)

连接详细信息:

如果有人能向我解释如何解决这个问题,我将不胜感激。

让我们用我自己的例子来验证你的例子。

  • 甲骨文 19c
  • Python 3.8.1
  • cx_Oracle 8.0

提示:数据库的 DNS 名称是 ODCGRC1R.XXXXX.DEV.CORP,它不同于服务器的主机名 scglvdoracd0006.xxxxx.dev.corp

1.Create 对其他模式具有 select 特权的用户

SQL> select instance_name , host_name from v$instance ;

INSTANCE_NAME         HOST_NAME
----------------------------------------------------
odcgrc1r              scglvdoracd0006.xxxxx.dev.corp

SQL> create user testpython identified by "Oracle_1234" ;

User created.

SQL> grant connect to testpython ;

Grant succeeded.

SQL> grant select any table to testpython ;

Grant succeeded.

2.From 我的笔记本电脑,连接以检查另一个架构中的 table ( sqlplus )

C:\python>tnsping ODCGRC1R.XXXXX.DEV.CORP:60995/odcgrc1r

TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 11-SEP-2021 12:00:25

Copyright (c) 1997, 2010, Oracle.  All rights reserved.

Parameterdateien benutzt:
C:\Programme\Oracle.2.0\client\network\admin\sqlnet.ora

Adapter HOSTNAME zur Aufl÷sung des Alias benutzt
Verbindungsversuch mit (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=odcgrc1r))(ADDRESS=(PROTOCOL=TCP)(HOST=180.22.128.47)(PORT=60995)))
OK (130 ms)

C:\python>sqlplus testpython/"Oracle_1234"@//ODCGRC1R.XXXXX.DEV.CORP:60995/odcgrc1r

SQL*Plus: Release 12.2.0.1.0 Production on Sat Sep 11 12:00:46 2021

Copyright (c) 1982, 2016, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

SQL> select count(*) from cpl_rep.frm_c001_status_cat ;

  COUNT(*)
----------
         4

SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

现在,让我们使用 cx_Oracle 来做同样的事情。下面是一个简单的测试程序

import cx_Oracle
import os 

host="ODCGRC1R.XXXXX.DEV.CORP"
port=60995
sid='odcgrc1r'
user='testpython'
password='Oracle_1234'
sid = cx_Oracle.makedsn(host, port, service_name=sid)

connection = cx_Oracle.connect(user, password, sid, encoding="UTF-8")
cursor = connection.cursor()
cursor.execute('select count(*) from CPL_REP.FRM_C001_STATUS_CAT')

for row in cursor:
    print(row)

所以,让我们执行它

C:\python>python testconn2.py
(4,)

* 替换 count*) 以打印所有行

C:\python>python testconn2.py
(1, 'Scheduled', 1, None, None, None, None)
(2, 'Waiting Execution', 1, None, None, None, None)
(3, 'Running', 1, None, None, None, None)
(4, 'Ended', 1, None, None, None, None)

总结

cx_Oracle 中访问不同架构中的其他 table 没有问题,只要您有相应的权限即可。在你的情况下,如果它由 SQL 开发人员工作,但它不在 Python 上,可能你没有连接到正确的 PDB。