I'm trying to connect oracle 11g with python
import oracledb
import os
user = 'system'
password = 'admin123'
port = 1521
service_name = 'xe'
oracle_server_addr = 'localhost'
conn_string = "{oracle_server_addr}:{port}/{service_name}".format(oracle_server_addr=oracle_server_addr, port=port, service_name=service_name)
print(conn_string)
with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
with conn.cursor() as cursor:
sql = """select sysdate from dual"""
for r in cursor.execute(sql):
print(r)
But I'm getting this error:
conn_string: localhost:1521/xe
Traceback (most recent call last):
File "src/oracledb/impl/thin/connection.pyx", line 353, in oracledb.thin_impl.ThinConnImpl._connect_with_address
File "src/oracledb/impl/thin/protocol.pyx", line 207, in oracledb.thin_impl.Protocol._connect_phase_one
File "src/oracledb/impl/thin/protocol.pyx", line 386, in oracledb.thin_impl.Protocol._process_message
File "src/oracledb/impl/thin/protocol.pyx", line 365, in oracledb.thin_impl.Protocol._process_message
File "src/oracledb/impl/thin/messages.pyx", line 1835, in oracledb.thin_impl.ConnectMessage.process
File "src/oracledb/impl/thin/buffer.pyx", line 845, in oracledb.thin_impl.Buffer.read_uint32
File "src/oracledb/impl/thin/packet.pyx", line 235, in oracledb.thin_impl.ReadBuffer._get_raw
File "src/oracledb/impl/thin/packet.pyx", line 588, in oracledb.thin_impl.ReadBuffer.wait_for_packets_sync
File "src/oracledb/impl/thin/transport.pyx", line 306, in oracledb.thin_impl.Transport.read_packet
File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
raise exc_type(_Error(message)) from cause
oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tmp/workspace/sql_ping/sql_ping.py", line 16, in <module>
with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 1134, in connect
return conn_class(dsn=dsn, pool=pool, params=params, **kwargs)
File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 523, in __init__
impl.connect(params_impl)
File "src/oracledb/impl/thin/connection.pyx", line 449, in oracledb.thin_impl.ThinConnImpl.connect
File "src/oracledb/impl/thin/connection.pyx", line 445, in oracledb.thin_impl.ThinConnImpl.connect
File "src/oracledb/impl/thin/connection.pyx", line 411, in oracledb.thin_impl.ThinConnImpl._connect_with_params
File "src/oracledb/impl/thin/connection.pyx", line 392, in oracledb.thin_impl.ThinConnImpl._connect_with_description
File "src/oracledb/impl/thin/connection.pyx", line 358, in oracledb.thin_impl.ThinConnImpl._connect_with_address
File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
raise exc_type(_Error(message)) from cause
oracledb.exceptions.OperationalError: DPY-6005: cannot connect to database (CONNECTION_ID=emkjRug2341/ymv7uTC0Bg==).
DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011
Context (Environment)
Both (database and client app are in the same host)
Database
- Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit (localhost)
- ubuntu
- user = 'system'
- password = 'admin123'
- port = 1521
- service_name = 'xe'
- host = 'localhost' (I also tried with local ip and 127.0.0.1)
App
- ubuntu
- python 3.10
- oracledb 2.0.1 & 2.0.0
- thin mode
No firewall, proxy, antivirus, etc. All in my ubuntu localhost
Attempts
# Works with java
In the same host, with the same database and thin mode, java <3 is able to connect without any problems. Code is here
# Works with dbeaver database ide
# oracledb pip library downgrade
I tried with all the versions since 1.3.1 and the error is almost the same
https://pypi.org/project/oracledb/#history
1.2.1, 1.3.2, 1.4.2
oracledb.exceptions.NotSupportedError: DPY-3010: connections to this database server version are not supported by python-oracledb in thin mode
# disable_oob= True
I tried this answer but the error is the same
# Similar unsolved questions
- DPY-4011: the database or network closed connection
- DPY-4011: the database or network closed the connection
- Error Connecting virtual machine to oracle, using python (error DPY-4011)
- What does 'DPY-6001: cannot connect to database' mean with python-oracledb?
- What does 'DPY-6005: cannot connect to database. Connection failed with "[Errno 61] Connection refused"' mean with python-oracledb
- Oracle DPY-6005: cannot connect to database. "[WinError 10061] No connection could be made because the target machine actively refused it"
- python-oracledb thin client returns DPY-6005
- https://github.com/oracle/python-oracledb/issues/234

You cannot using thin mode as OracleDB does not support Oracle 11.
From the Python OracleDB PyPi page:
Either use thick mode or upgrade your database to at least Oracle 12.1.