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.
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()
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.
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.
Related
I am unable to connect to our enterprise Oracle Db using python 3/cx_Oracle.
Installed are:
python 3 -32 bit
cx_Oracle
Oracle Client 12.1.0.2.0
My connection string attempt is:
import cx_Oracle
conn = cx_Oracle.connect(user='user', password='pwd', dsn='working_dsn')
My PATH variable includes the direct path to my working Oracle library (works using SQL Dev
Error message is:
cx_Oracle.DatabaseError: DPI-1050: Oracle Client library is at version 0.0 but must be at version 11.2 or higher
I have researched the Orcale installation instructions and have found no way to connect. I have previously tried with no success, had my computer reimaged and Oracle reinstalled to ensure only one version of Oracle and still no success. I need to move from R to Python and this is the last piece I need to make the switch. I am able to connect with R using JDBC driverclass/dbConnect.
If cx_Oracle wont work, is there another option for connecting to Oracle from Python3?
Any thoughts suggestions or places to look? Other connection types used?
Thanks in advance.
pip3 install cx_Oracle
first method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Second method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Third method
makedsn(IP/HOST, PORT, TNSNAME)
dsn = cx_Oracle.makedsn('localhost','1523','orcl')
db = cx_Oracle.connect('root','root',dsn)
The error you are getting suggests that you have an older version of the Oracle client installed on your machine. Search the directories of your PATH environment variable for OCI.DLL. If you find an older version you'll need to remove or rename it -- just be aware that whatever application put the file there will stop working!
Another possibility is to go to a command prompt and do the following
PATH=my_path_to_instant_client;%PATH%
python test_connect.py
Finally, make sure that if your Python is 32-bit, so is your instant client installation, and if your Python is 64-bit, make sure that your instant client installation is also 64-bit.
Some days ago, I was asked to develop a Python application capable of connecting to a Oracle DB. Since I already have an Oracle client installed (version 12.2.0), I just pip installed cx_Oracle and tried to establish a connection using below code:
import pandas as pd
import cx_Oracle
connection = cx_Oracle.connect('username/password#service_as_described_in_tnsnames.ora')
cur=connection.cursor()
input("Press Enter to continue...")
cur.execute('select* from MY_PRETTY_TABLE')
for line in cur:
print()
cur.close()
connection.close()
But when trying to run it, I got the error "DPI-1050: Oracle Client library must be at version 11.2 or higher". After googling it, I found this answer, and tried to change my code to:
my_dsn = cx_Oracle.makedsn("host",port,sid="sid")
connection = cx_Oracle.connect(user="user", password="password", dsn=my_dsn)
cursor = connection.cursor()
querystring = "SQL query"
cursor.execute(querystring)
But still, same error. It's important to notice that I have already used Oracle DB client in this same machine, to connect a DB with Power BI.
Also, if it can be helpful, my paths are setted as:
C:\instantclient_12_1
C:\Users\oracle2\product\12.1.0\client_1
C:\Users\oracle2\product\12.1.0\client_1\bin
C:\Users\oracle\product\12.2.0\dbhome_1\bin
That error implies that you have another older version of the Oracle client installed somewhere earlier in your PATH. You should do a search for OCI.DLL on your machine (using where.exe or the dir command) and either move or remove any unnecessary copies or adjust PATH as needed. Some older applications stuffed OCI.DLL in C:\Windows\System32 improperly, for example.
This problem had to do with Oracle Instant client Version 19.3.0.0.0.
I uninstalled it and installed the previous version Oracle Instant client 12.2.0.1.0 to and it worked.
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Important things to do before you try above:
From Windows command prompt
c:> where oci.dll
make sure you find only one entry and remove the redundant ones.
Ensure you set the environment path to the newly installed client.
I wanna connect to Oracle 8i Database using Python2.7 or Python3.6 as I am not an Oracle guy so I need your help on this.
I am having following scenario:
My Database server is located at remote location.
I have to connect with that database through either version of Python2.7 or Python3.6.
After connection I just wants to do as normal queries.
Things which I have already done is:
cx_Oracle library 6.2 version installed.
Oracle instant Client libraries installed and using these libraries I am able to connect from Oracle 9i to Oracle 12c.
Now I just wants to make connection with Oracle 8i database.
thank you.
Uh, Oracle 8 ... where did you manage to find that fossil?
Anyway: this page says that you should use "OJDBC and JayDeBeApi" which works with databases
supported by Oracle's JDBC drivers (currently 8.1.7 to 11.2.0.2.0)
There's some more info, so - have a look.
I'm making queries from a MS SQL server using Python code (Pymssql library) however I was wondering if there was any way to make the connection secure and encrypt the data being sent from the server to python?
Thanks
Yes, it can.
You need FreeTDS which supports SSL via OpenSSL. If you happened to use Linux (or Docker on Windows), it's quite easy to install standalone FreeTDS in Debian:
apt-get update
apt-get install freetds-bin freetds-dev
pip install pymssql
Don't use pymssql with bundled FreeTDS library, it does not support SSL apparently. The bundled library is used when you set env variable PYMSSQL_BUILD_WITH_BUNDLED_FREETDS=1 before installing pymssql.
pymssql certainly claims to be able to work with encrypted connections to the SQL Server (via OpenSSL). One reason why some might believe it to be impossible is that Windows releases of pymssql versions prior to 2.1.2 were shipped with pymssql statically linked to FreeTDS for unencrypted connections only.
Starting with version 2.1.2, the Windows release of pymssql is shipped dynamically linked to FreeTDS so it can support both unencrypted connections (via FreeTDS alone) or encrypted connections (via FreeTDS and OpenSSL). For details – and an important update re: versions 2.1.3 and later – see the related answer here.
If you want to connect SQL server using secured connection using pymssql then you need to provide "secure" syntax in your host..
for e.g.
unsecured connection host : xxx.database.windows.net:1433
secured connection host : xxx.database.secure.windows.net:1443
Unfortunately there's no way, but you could use pyodbc.
At work we have Oracle 7. I would like to use python to access the DB.
Has anyone done that or knows how to do it?
I have Windows XP, Python 2.6 and the cx_oracle version for python 2.6
However, when I try to import cx_oracle i get the following error:
ImportError: DLL load failed the module could not be found
Any help is appreciated!
Matt
cx_Oracle is currently only being provided with linkage to the 9i, 10g, and 11i clients. Install one of these clients and configure it to connect to the Oracle 7 database using the proper ORACLE_SID.
Make sure you have the location of the oracle .dll (o files set in your PATH environment variable. The location containing oci.dll should suffice.
I was running into that same problem at work. I finally dropped trying to use cx_Oracle and went with adodbapi. It worked with Oracle 8.
If you have ODBC configured then you can use it. It is available with ActivePython or as win32 extensions. You will obtain connection with:
connection = odbc.odbc('db_alias/user/passwd')
Optionally you can use Jython and thin JDBC client. Instalation of client is not required. With Jython you have access to db via db url:
db = DriverManager.getConnection(db_url, usr, passwd)
where db_url looks like:
jdbc:oracle:thin:user/passwd#machine_ip:port:dbname