I am maintaining some code where i came about a curious connection string of following type to an oracle database (from redhat linux):
import cx_Oracle
cx_Oracle.Connection("username/password")
Notably no DSN is specified; User name and password are enough (the connection is working).
How is this possible? How does cx_oracle know where to connect to? Is there some default value/environment variable that is used if no DSN is given? The way I understood the documentation, the DSN is a mandatory argument.
CodePudding user response:
cx_oracle has method to create 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()
CodePudding user response:
As Devyl mentioned, if you have ORACLE_SID or TWO_TASK environment variables set, they maybe used to make a connection.
E.g. see this answer https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:89412348059