How to make QOCI driver available for PyQt5 - python

I'm trying to use QOCI driver to connect to Oracle Database, but the driver is not available. For this simple code:
from PyQt5 import QtSql
QtSql.QSqlDatabase.isDriverAvailable('QOCI')
I get False.
I tried checking the PATH environmental settings, and I see that there is TNS_ADMIN variable set to folder with driver file oci.dll:
C:\Atena\instantclient_11_2
but it's still unavailable.
I tried downloading the driver from Oracle site, setting it up according to Oracle instructions (changing the PATH variable to the new driver folder, copying TNSnames.ora file etc.), but still the driver is unavailable.
I want to have the driver available for PyQt5, load it, and the use it to connect to Oracle database.

The OCI.DLL is included for example in instantclient-basic-windowsXXXXX.zip
Assume you have unzipped file to
C:\ORACLE\IC\12201\instantclient_12_2
to make it available for other applications, set in user environment
ORACLE_HOME=C:\ORACLE\IC\12201\instantclient_12_2
PATH=%PATH%;%ORACLE_HOME%
with this setting any application using OCI.DLL should find it.
Of course you have to install the right bitsize - i.e. if you application is 32bit, you need to install a 32bit Instant Client - if it is a 64bit App, you need 64bit instant client.
So - as far as I can see you need to build the QOCI manually. To do that, you need definetly the SDK package for instant client.
Sources on the web state that you need to include headers and libs from SDK - unfortunatly it looks like the QOCI still depends on Oracle Client 11.X - not instant client but regular client.
following libraries need to be used (last one is for client 12.x)
oci.lib ociw32.lib oraocci12.lib
on regular client
%ORACLE_HOME%\oci\lib\msvc
on instant client
%ORACLE_HOME%\sdk\lib\msvc
following headers need to be included
oci.h
on regular client
%ORACLE_HOME%\oci\include
on instant client
%ORACLE_HOME%\sdk\include
Unfortunatly there is no much support for MinGW so I cannot do that.
the "configure" command to prepare everything needs to include
-sql-oci -plugin-sql-oci

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.

What is necessary to connect Python to Oracle Database

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.

Flask app fails to render when deploying code to Azure Web with a database connection, but works fine from local server

I have an Azure web app with a Flask template. I want it to connect to a SQL database. I made one. I installed pymssql. For testing purposes I added to the views.py in the root folder:
import pymssql
conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin#mydb', password='secret', database='mydb')
I first tested locally on my kubuntu box. To make sure I was getting a connection, I entered the wrong password, and the server threw an error. I entered it correctly and the error disappeared. Cool. Also the SQL dashboard on the azure portal reported sucessfull connections. So then I pushed my changes (including updating the requirements.txt) to my github repo and it was sucked into my web application. When I tried running the web app, on the index page:
The page cannot be displayed because an internal server error has
occurred.
After turning on and inspecting the detailed logs all I get it:
HTTP Error 500.0 - Internal Server Error
The page cannot be displayed because an internal server error has occurred.
Most likely causes:
IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.
IIS. What's that doing here? I thought MS was trendy and hosted this service on a linux box 0_0
I remove the second line
conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin#mydb', password='secret', database='mydb')
and the error disappeared. If I run a server locally, which still uses the Azure SQL server, I don't have any issues.
This seems to be the same issue I have:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e20bb2a3-bdbe-4e49-8b92-b36fe50577da/having-trouble-deploying-flask-app-into-azure-with-azure-database?forum=windowsazurewebsitespreview&prof=required
but they say whether they actually got it working with a SQL database...
what do people think?
Thanks
my requirements.txt
alembic==0.7.7
azure==0.11.1
Flask==0.10.1
Flask-Migrate==1.5.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.0
futures==3.0.3
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.1
MarkupSafe==0.23
pymssql==2.1.1
python-dateutil==2.4.2
six==1.9.0
SQLAlchemy==1.0.8
Werkzeug==0.10.4
wheel==0.24.0
Per my understanding, Azure Web sites are hosted on Windows Server 2012 VM by default, which would not install FreeTDS and also we don't have permission to install it. And the pymssql is based on FreeTDS.
So we can use pyodbc to connect the Azure SQL as a workaround way to handle it in ease.
I was able to successfully connect my Python code to a SQL Database on Azure using the pymssql 2.1 library.
I don't know if this is what is causing the problem but I did include a couple of extra parameters in my connection string... maybe that will help. I specified the driver explicitly and the Encrypt parameter since SSL is always enabled for SQL Azure (I believe)
myConnection = pyodbc.connect('Driver={SQL Server};'
'Server=tcp:1234567.database.windows.net,1433;'
'Database=MyAzureDatabase;'
'Uid=geekgirl#123456;Pwd=abcdef;'
'Encrypt=yes')
If that doesn't work, maybe try adding some error handling around the connect statement to try and get a more specific error message.
Fingers crossed!
I had this problem, but I eventually realized it was because I was using the 64-bit version of Python in Visual Studio, but Azure only supports the 32-bit version.
I was seeing:
DLL load failed: %1 is not a valid Win32 application.
Once I switched to installing and using the 32-bit version I was able to use Flask and pyodbc with no problems.

pymssql: How to use windows authentication when running on a non-windows box

Is there a way for python to connect to MS SQL Server using Windows Authentication, even when not running the python app on a windows box?
I'm trying to do this with pymssql, but the examples mostly seem to assume that you're running on windows.
If there is a way to make this connection using some other library, please feel free to suggest, but I do like how pymssql is simple to install and deploy via pip.
I want to connect to 2005/2008 databases, and I'm running Ubuntu 13.04 (but I can upgrade to a later Ubuntu if it makes a difference)
SOLUTION:
It turns out that pymssql can connect to my database via my windows username and password. But to do so, I need to pass the actual username/password like this:
pymssql.connect(host, 'THEDOMAIN\\theusername', 'thepassword', db)
The solution EkoostikMartin provided is still good though (if you do not want to store a password somewhere, which is probably the point to windows auth anyway)
You can use the SQL Server ODBC driver for linux, and set up Kerberos.
See this article - http://technet.microsoft.com/en-us/library/hh568450.aspx

Install MYSQLdb python module without MYSQL local install

I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.

Categories

Resources