如何使用 cx_Oracle 连接到 Oracle 12c 数据库

How to connect to Oracle 12c Database using cx_Oracle

sqlplus sys/Oracle_1@pdborcl as sysdba;

我正在使用此命令从命令提示符连接到 Oracle 12c。 我如何使用 cx_Oracle 连接到数据库。我是 Oracle 数据库的新手。

您可以在此处找到文档 cx_Oracle docs

要查询数据库,请使用以下算法

import cx_Oracle

dsn = cx_Oracle.makedsn(host, port, sid) 
connection = cx_Oracle.connect(dsn,mode = cx_Oracle.SYSDBA)
query = "SELECT * FROM MYTABLE"
cursor = connection.cursor()
cursor.execute(query)
resultSet=cursor.fetchall()
connection.close()

以上代码用于从连接到上述 dsn 的 MYTABLE 中获取数据。 最好通过 cx_Oracle 文档。

我认为这相当于您发布的 sqlplus 命令行:

import cx_Oracle

connect_string = "sys/Oracle_1@pdborcl"
con = cx_Oracle.connect(connect_string,mode=cx_Oracle.SYSDBA)

我尝试使用 non-container 数据库而不是 pdb,所以我无法验证它是否适用于 pdb。您可能不想作为 sys 作为 sysdba 连接,除非您知道您需要那种安全级别。

鲍比