0

Consider below code:- Now the below works

connection = cx_Oracle.connect(dsn = 'DSNAME')

But when I use below format for SqlAlchemy it doesn't work, I get TypeError: Invalid arguments dsn passed:

connection = create_engine('oracle+cx_oracle://' , dsn = 'DSNAME')
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49

2 Answers2

1

SQLAlchemy requires a database connection URI, there is an article about it on their documentation. It requires the format

oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]

Have you tried the following?

connection = create_engine('oracle+cx_oracle://' + 'DSNAME')
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
1

it seems pretty old post, I tried with below code, it works . hope this helps. providing empty username/passwd, they are read from wallet, location is mentioned in sqlnet.ora

tnsnames.ora:

t1 = (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ip)(PORT=1521)(KEY=dbpdb1))(CONNECT_DATA=(SERVICE_NAME=dbsvc1.oracle.com))).

sqlnet.ora:

WALLET_LOCATION = (SOURCE = (METHOD = FILE) (METHOD_DATA = (DIRECTORY = $walletdir) ) ) SQLNET.WALLET_OVERRIDE = TRUE

from sqlalchemy import create_engine
cstr='oracle://:@t1'
print(cstr)
engine = create_engine(
        cstr,
        convert_unicode=False,
        echo=True
        )
s='select * from emp'
conn = engine.connect()
result = conn.execute(s)
for row in result:
    print (row)