4

I am trying to build an application in python which will use Oracle Database installed in corporate server and the application which I am developing can be used in any local machine.

Is it possible to connect to oracle DB in Python without installing the oracle client in the local machine where the python application will be stored and executed?

Like in Java, we can use the jdbc thin driver to acheive the same, how it can be achieved in Python.

Any help is appreciated

Installing oracle client, connect is possible through cx_Oracle module. But in systems where the client is not installed, how can we connect to the DB.

6
  • Perhaps pypi.org/project/JayDeBeApi - use Java JDBC driver in Python. You must still install JayDeBeApi module though (instead of Oracle Client), and download the driver file (JAR) from Oralce's site. Commented Sep 4, 2019 at 14:40
  • I tried to install this module but getting below error error: Microsoft Visual C++ 14.0 is required. Commented Sep 4, 2019 at 17:04
  • See this article - python windows compilers. You are using Windows, so you need to install Microsoft C++ compiler which is compatible with your versionof Python. Commented Sep 4, 2019 at 19:14
  • python other functionalities are working perfectly fine. Even cx_Oracle module also working fine. Getting error only while installing the JayDeBeApi module. Commented Sep 4, 2019 at 19:33
  • If all the target machines are Windows, you can put the Oracle Instant Client libraries in the same directory as the cx_Oracle shared library. Commented Sep 6, 2019 at 5:06

4 Answers 4

6

The python-oracledb driver (that superseded cx_Oracle) does not need Oracle Client libraries. It only takes a few megabytes. Installation is simple:

python -m pip install oracledb

It supports the Python DB API so usage is the same as cx_Oracle, e.g.:

import getpass
import oracledb

un = 'cj'
cs = 'localhost/orclpdb1'
pw = getpass.getpass(f'Enter password for {un}@{cs}: ')

with oracledb.connect(user=un, password=pw, dsn=cs) as connection:
        with connection.cursor() as cursor:        
        sql = """select systimestamp from dual"""
        for (r,) in cursor.execute(sql):
            print(r)

See the python-oracledb documentation and samples.

Also see the python-oracledb release announcement.

The cx_Oracle driver is obsolete and should not be used for any projects.

Sign up to request clarification or add additional context in comments.

Comments

5

You can use JDBC

"""
Connect from Python to Oracle via JDBC
Get JDBC-driver here: https://download.oracle.com/otn/utilities_drivers/jdbc/193/ojdbc8-full.tar.gz
Python 3.7.4
conda install -c conda-forge jaydebeapi==1.1.1 --force-reinstall -y
conda install -c conda-forge JPype1==0.6.3 --force-reinstall -y
"""
import jpype
import jaydebeapi

JHOME = jpype.getDefaultJVMPath()
jpype.startJVM(JHOME, '-Djava.class.path=/ojdbc8-full/ojdbc8.jar')
con = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver',
                         'jdbc:oracle:thin:user/pass@host_ip:1521:SID')
cur = con.cursor()
cur.execute('select dummy from dual')
r = cur.fetchall()
print(r[0][0])
cur.close()
con.close()

1 Comment

this just substitutes one enormous dependency (java) for another (oci). If the target already has java, ok, but if you're trying to limit dependencies, this is definately a frying pan/fire situation.
2

It is not correct that java can connect to oracle without any oracle provided software.

It needs a compatible version of ojdbc*.jar to connect. Similarly python's cx_oracle library needs oracle instant-client software from oracle to be installed.

Instant client is free software and has a small footprint.

Comments

0

Installing Oracle client is a huge pain. Could you instead create a Webservice to a system that does have OCI and then connect to it that way? This might end being a better solution rather than direct access.

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.