简体   繁体   English

没有 DSN 的 cx_oracle 连接

[英]cx_oracle connection without DSN

I am maintaining some code where i came about a curious connection string of following type to an oracle database (from redhat linux):我正在维护一些代码,其中我遇到了以下类型的奇怪连接字符串到 oracle 数据库(来自 redhat linux):

import cx_Oracle
cx_Oracle.Connection("username/password")

Notably no DSN is specified;值得注意的是,没有指定 DSN; User name and password are enough (the connection is working).用户名和密码就足够了(连接正常)。

How is this possible?这怎么可能? How does cx_oracle know where to connect to? cx_oracle 如何知道连接到哪里? Is there some default value/environment variable that is used if no DSN is given?如果没有给出 DSN,是否有一些默认值/环境变量被使用? The way I understood the documentation, the DSN is a mandatory argument.根据我对文档的理解,DSN 是一个强制性参数。

cx_oracle has method to create dsn makedsn cx_oracle 具有创建 dsn makedsn的方法

dsn = cx_Oracle.makedsn(host, port, sid=None, service_name=None, region=None, sharding_key=None, super_sharding_key=None)

with cx_Oracle.connect(user=user, password=password,
                       dsn=dsn,
                       encoding="UTF-8") as connection:
    cursor = connection.cursor()
    cursor.execute("insert into SomeTable values (:1, :2)",
                   (1, "Some string"))
    connection.commit()

As Devyl mentioned, if you have ORACLE_SID or TWO_TASK environment variables set, they maybe used to make a connection.正如 Devyl 提到的,如果您设置了 ORACLE_SID 或 TWO_TASK 环境变量,它们可能用于建立连接。

Eg see this answer https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:89412348059例如看到这个答案https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:89412348059

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

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