What is necessary to connect Python to Oracle Database - python

I have been trying to connect Python 3.7.1 to TOAD 11, with a client version 10.2.
When I try running the code below
import cx_Oracle
connection = cx_Oracle.connect('myusername/mypassword#orcl')
cursor = connection.cursor()
querystring = "select * from TABLE_NAME"
cursor.execute(querystring)
I get the following error:
DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "C:\ORACLE\PRODUCT\10.2.0\CLIENT_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help
I have been trying to download a 64 bit client, but I don't understand why I need one since TOAD and it's current client are 32-bit. The issue is that with my company, it does not say the bits in the description whenever something is requested to be installed (due to the security in the network, I cannot download anything unless it is approved by the IT department, and it can only be downloaded through their server). I attempted to download the instant client 11g, but it seems it was installed but the 10.2 version it's still the only one I can find in my machine. I cannot see where it is located anywhere, so I cannot even set an environment variable for it, but I can see it in the "uninstall wizard", but it doesn't show it's location. I don't even know if 11g is 64 bit or 32 bit.
Has anybody fixed this problem before?
Is my code off and maybe that's why its not working?

The error message is saying you need a 64-bit Oracle Client because Python is 64-bit.
For cx_Oracle you need Oracle client libraries version 11.2 or later. You can get the libraries as part of Oracle Instant Client from http://www.oracle.com/technetwork/topics/winx64soft-089540.html On Windows you will also need the Redistributable noted on that download page.
Also see the cx_Oracle installation instructions at https://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-windows
You will have to work with your IT people to make this happen.

Related

Connect to Oracle database using python from a Linux server (lxv)

In my local machine
I have created a script in python that retrieves data from an Oracle database.
The connection to the DB is done using cx_Oracle:
con = cx_Oracle.connect (username, password, dbService)
When using SQL developer the connection is established using custom JDBC.
Replicate procedure on a Linux server.
I have created a python virtual environment with cx-Oracle pip installed in it.
I have Oracle Client 19.3.0 installed in the server, and the folder instantclient is in place.
When I try to execute the python script as is I get the following error.
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle
Client library:
DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
I assumed that the problem was the Oracle path which is not the one that python expected. So, I added this extra line of code pin-pointing the path where the Oracle libraries are located.
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
This leads to a different error:
cx_Oracle.DatabaseError: Error while trying to retrieve text for error
ORA-01804
Any clues?
The answer to my question was indicated by the ORA-1804 message.
According to the Oracle initialization doc: https://cx-oracle.readthedocs.io/en/latest/user_guide/initialization.html#usinginitoracleclient
Note if you set lib_dir on Linux and related platforms, you must still
have configured the system library search path to include that
directory before starting Python.
On any operating system, if you set
lib_dir to the library directory of a full database or full client
installation, you will need to have previously set the Oracle
environment, for example by setting the ORACLE_HOME environment
variable. Otherwise you will get errors like ORA-1804. You should set
this, and other Oracle environment variables, before starting Python,
as shown in Oracle
Even though defining the ORACLE_HOME should be done before starting Python (according to Oracle documentation) it is possible to do so by modifying the python script itself. So before the oracle client initialization command the following commands had to be added:
import os
# Setup Oracle paths
os.environ["ORACLE_HOME"] = '/apps/oracle/product/19.3.0'
os.environ["ORACLE_BASE"] = '/apps/oracle'
os.environ["ORACLE_SID"] = 'orcl'
os.environ["LD_LIBRARY_PATH"] = '/apps/oracle/product/19.3.0/lib'
import cx_Oracle
# Initialize Oracle client
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
The cx_Oracle initialization doc points out that on Linux init_oracle_client() doesn't really do what you think it does. You must still set the system library search path to include the Oracle libraries before the Python process starts.
Do I understand correctly that the machine with Python has both the DB installed and Instant Client??
If you do want Python to use the Instant Client libraries, then set LD_LIBRARY_PATH to its location and do not set ORACLE_HOME.
If you have a full Oracle DB installation on the machine with Python, then you can delete Instant Client. You need to set ORACLE_HOME, LD_LIBRARY_PATH and whatever else is needed before starting Python - in general run source /usr/local/bin/oraenv. This should set the system library search path to include /apps/oracle/product/19.3.0/lib . A code snippet like this (untested) one may help: export ORACLE_SID=ORCLCDB;set ORAENV_ASK=NO; source /usr/local/bin/oraenv. Make sure the Python process has read access to the ORACLE_HOME directory.
The cx_Oracle installation guide discusses all this.

Cx Oracle 'TNS could not resolve the connect identifier'

I am connecting to an Oracle DB using LDAP and the Python cx_Oracle library. I have proper sqlnet.ora, ldap.ora and tnsnames.ora files. On my Windows machine everything works fine using the 12.1 Oracle client and the following Python code:
import cx_Oracle
connection = cx_Oracle.connect(user/password#db, mode=cx_Oracle.SYSDBA)
I have installed the Oracle instant client 12.1 on my Linux machine (Debian) following the zip file installation method advised on the Oracle website (at the bottom of https://www.oracle.com/be/database/technologies/instant-client/linux-x86-64-downloads.html).
ldconfig correctly lists the oracle client libraries and their paths.
I copy the exact same sqlnet.ora, ldap.ora and tnsnames.ora files from my Windows machine to my Linux machine
in /opt/oracle/instantclient_12_1/network/admin, as advised by the cx_Oracle documentation.
Now running the above Python code on my Linux machine I have the following error:
cx_Oracle.DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
I tried as well setting the environment variable TNS_ADMIN=/opt/oracle/instantclient_12_1/network/admin and ORACLE_HOME=/opt/oracle/instantclient_12_1/ with no success neither
Could any of you help me debugging this and understand why the client config on my Linux machine does not work?
Installed the latest 19 client and everything works as expected.

Firebird x32-x64 bits

I have developed a Python executable programm (with PyInstaller).
This executable use Firebird.
My computer is 64bits, so as my Python and my libraires then.
My executable works perfectly with Firebird 64bits.
The issue is that I want my executable to work on other computers that have only Firebird 32bits installed.
For now, when I try on an other computers the log tells me that (Sorry I have only the picture because my client only send me this and not the 'text' error) :
Is there a way to make it work with Firebird x32 ?
Thanks
You application need client libraries of Firebird.
[Application] <--> [Firebird client libraries] <==> (local or remote connection) <==> [Firebird client libraries] <-> [Firebird server]
If your application is Win64, then it needs Win64 client libraries of Firebird to make connection to ANY Firebird server.
So you need to distribute and install client libraries of Firebird matching your application architecture together with your app. Or you may require users to download and install client part of Firebird with the required architecture, before installing your app.
See also:
connecting to firebird server from client
https://firebirdsql.org/manual/qsg10-client-only-install.html
https://firebirdsql.org/file/documentation/reference_manuals/driver_manuals/odbc/html/fbodbc205-download-fbclient.html
https://firebirdsql.org/file/documentation/reference_manuals/user_manuals/html/qsg3-installing.html
If your app is 32bit than you need 32bit fb client dll/so to load, even on 64bit platform. You can connect on any compatible fbserver(regardless 64 or 32bit). So fb client lib(dll) issues are those you need to resolve. If you use fb as embedded library you need only one dll, and it must be compatible with your app binary interface and on 32bit system you need 32bit fbclient.dll to load. I hope this helps.

Python cx_Oracle connection DLL error

I have configured the Oracle client and try with the below code, But error prompted from the code.
import os
os.chdir("C:\\oreclient_install_dir\\instantclient_12_2")
import cx_Oracle
ORACLE_CONNECT = "user/pass#(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521)))(CONNECT_DATA=(SID=sid)(SRVR=DEDICATED)))"
orcl = cx_Oracle.connect(ORACLE_CONNECT)
print("Connected to Oracle: " + orcl.version)
Error :
Python.exe - Entry Point Not Found
The Procedure entry point K32GetModuleBaseNameA could not be located in the dynamic link library KERNEL32.dll
I am using Server 2008 32 bit as client machine and remotely connect to Oracle Data Base 12c Enterprise Edition Release 12.2.0.1.0 - 64bit
Tried both Python 2.7 and 3.6.
Instant client version is "instantclient_12_2"
Appreciate if you can give me a feedback on this.
Thanks,
Tharindu Panagoda

"Data source name not found and no default driver specified" with Access ODBC

My Softwares:
Python 3.4 -64 bit
PyODBC 64 bit
MS office package Installed (32 bit)
Problem:
Now, I try to access MS Access 2010 installed in my computer using PYODBC. It does not work regardless of what I try. My error is always this:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I already looked at:
pyodbc and ms access 2010 connection error
Cannot connect to Access DB using pyodbc
It is suggested that it is 32-64 bit problem which I am sure it is. Is there some modification that can be done to make it work without installing 32 bit python and 32 bit pyodbc? I checked this website.Using ODBC (32-bit and 64-bit) on 64-bit Windows which explains about accessing the control panel to modify ODBC connnection and/or drivers. However, I don't know much about windows database connection internals to commence some changes.
Is there something that I can do to make it work?
Is there some modification that can be done to make it work without installing 32 bit python and 32 bit pyodbc?
Not really. If you have 32-bit Office installed then you have the 32-bit version of the Access Database Engine (a.k.a. "ACE"), and only 32-bit applications can use it.
The installer for the 64-bit version of the Access Database Engine will abort if it detects 32-bit Office components. There is a way to force the installer to continue, but that is not recommended because it can apparently break Office.
Bottom Line: The "bitness" of your application must match the "bitness" of the installed Access Database Engine. So, practically speaking, your options are
Use 32-bit Python and pyodbc, or
replace your 32-bit Office with the 64-bit version.

Categories

Resources