问题
I am trying to connect to database using cx_Oracle module i am getting below error
server_IP = ipaddress:1221/xyz
try:
db = cx_Oracle.connect('username', 'password', server_IP)
print db
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 1017:
print('Please check your credentials.')
else:
print('Database connection error: %s'.format(e))
raise
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
My question do i need to install any oracle client in linux or how to configure it in linux so that i won't get this error. please help What are the preconditions are required in linux to use cx_Oracle module
回答1:
Please check the syntax of your call to cx_Oracle.connect
. It takes username, password and DSN OR one argument that has it all.
For example
con = cx_Oracle.connect('username/password@ipaddress/xyz')
or to construct a full DSN:
ip = 'ipaddress'
port = 1221
SID = 'xyz'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
db = cx_Oracle.connect('username', 'password', dsn_tns)
Using Python With Oracle
回答2:
To answer the first question: yes, you need to have an Oracle client installed on your machine in order to use cx_Oracle. You can install the full client but it is a lot easier to use the instant client. If you are using an RPM based Linux distribution you can use the RPMs which simplify things considerably. Go here for the instant client:
http://www.oracle.com/technetwork/database/features/instant-client/index.html
The error you are getting suggests a configuration issue, though. Make sure that the Oracle client you use and the version of cx_Oracle you use are compatible with each other. In other words, if Python is 64-bit, your Oracle client should also be 64-bit. If Python is 32-bit, your Oracle client should be 32-bit. Check the environment variables ORACLE_HOME (if using a full Oracle client), PATH and LD_LIBRARY_PATH. This is one of the advantages of using the instant client RPMs -- no environment variables are needed at compile time and none at run-time either!
One last comment: it looks like you are using EZ Connect syntax to connect to the database. The default port for the listener is 1521, not 1221. If you are using the default port you can simply leave out that section (in other words ipaddress/xyz as noted by Martin).
来源:https://stackoverflow.com/questions/40154200/cx-oracle-interfaceerror-unable-to-acquire-oracle-environment-handle-in-linux