Python connection to Oracle database - python

I am writing a Python script to fetch and update some data on a remote oracle database from a Linux server. I would like to know how can I connect to remote oracle database from the server.
Do I necessarily need to have an oracle client installed on my server or any connector can be used for the same?
And also if I use cx_Oracle module in Python, is there any dependency that has to be fulfilled for making it work?

You have to Install Instance_client for cx_oracle driver to interact with remote oracle server
http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html.
Use SQLAlchemy (Object Relational Mapper) to make the connection and interact with Oracle Database.
The below code you can refer for oracle DB connection.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('oracle+cx_oracle://test_user:test_user#ORACSG')
session_factory = sessionmaker(bind=engine, autoflush=False)
session = session_factory()
res = session.execute("select * from emp");
print res.fetchall()

Yes, you definitely need to install an Oracle Client, it even says so in cx_oracle readme.txt.
Another recommendation you can find there is installing an oracle instant client, which is the minimal installation needed to communicate with Oracle, and is the simplest to use.
Other dependencies can usually be found in the readme.txt file, and should be the first place to look for these details.

Related

Firebird connection on a local database impossible within a Python script

I can connect to my Firebird database using Firebird ISQL Tool (Firebird 3.0.4) with the following command:
connect "C:\Documents\database.db" user 'USER' password 'PASSWORD';
When I want to do it in a Python script (Python v3.7.7 on a Windows10 64 bits), in a virtual environment including fdb v2.0.1 or even firebirdsql v1.1.3, I can't and I systematically get an error.
import fdb
con = fdb.connect(database="C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "xnet://Global\FIREBIRD".', -902, 335544721)
or
con = fdb.connect(host='localhost', database="D:\Documents\database.db", user= 'USER' password= 'PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)
or
con = fdb.connect(dsn="localhost:C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError: ('Error while connecting to database:\n- SQLCODE:
-902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)
or
import firebirdsql
con = firebirdsql.connect(host='localhost', database="D:\Documents\database.db", user='USER' password='PASSWORD'')
If you have any idea you are welcome as I am stuck.
The error indicates that the fbclient.dll loaded by FDB does not provide Firebird Embedded, and that you don't have Firebird Server running on your machine.
To address this you must either:
Start Firebird Server (by starting its service or running firebird -a)
If you want to use Firebird Embedded instead of Firebird Server, point FDB to a fbclient.dll that provides Firebird Embedded
Point 2 can be done in several ways. In my answer I'm assuming use of Firebird 3 installed in C:\Program Files\Firebird\Firebird-3.0.5.33220-0_x64. Since Firebird 3, a normal Firebird Server install also provides Firebird Embedded. To point to a Firebird Embedded, you can do the following:
Add the Firebird installation directory to the PATH environment variable (make sure it is listed before %SystemRoot\System32/C:\Windows\System32). On a normal Firebird installation, the fbclient.dll without Firebird Embedded is installed in the System32 folder, and if that gets loaded, you can't use Firebird Embedded.
Use fdb.load_api to load the client library:
fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
This needs to be done before the first use of fdb.connect, otherwise the library found through the normal search path will be used
Specify the client library using the fb_library_name connection property:
con = fdb.connect(dsn='C:/path/to/yourdatabase.fdb', user='sysdba', password='masterkey',
fb_library_name='C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
This property needs to be specified on the first connection made using FDB. Although the existence of the property would suggest this is 'per connection', FDB will always use the first client library loaded (in essence, it works as if you called load_api just before the connect).
If you're using Firebird 2.5 or earlier, you will need to download the specific Firebird 2.5 Embedded package, and point to its fbembed.dll instead of fbclient.dll. For Firebird 2.5 Embedded, adding its location to the path will not work unless you rename or copy its fbembed.dll to fbclient.dll.

Error connecting to local oracle database with cx_Oracle in Jupyter

I want to connect to local database with cx_Oracle but it throws an error:
DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help
I can connect to database with sqlplus and SQL Developer, but it doesnt work with Python cx_Oracle in Jupyter.
import cx_Oracle
host = 'localhost'
port = 1521
SID = 'xe'
dsn_tns = cx_Oracle.makedsn(host, port, SID)
connection = cx_Oracle.connect('user', 'passwd', dsn_tns)
Is it possible I messed sth. with environment variables or client installation?
echo ${ORACLE_HOME};
/u01/app/oracle/product/11.2.0/xe
echo ${LD_LIBRARY_PATH};
/usr/lib/oracle/12.2/client64/lib
I installed client in /usr/lib/oracle/12.2/client64/lib
You may as well update cx_Oracle. The DPI-1047 message is using text that was updated in recent versions. This won't actually solve your problem
Keep it clean. Don't set ORACLE_HOME if you are using Oracle Instant Client.
I suspect your environment variables aren't being propagated down to cx_Oracle.
With a 64-bit XE, you shouldn't need to install Instant Client in your case (because your cx_Oracle message tells me it is 64-bit because the error is looking for a 64-bit Oracle client). cx_Oracle can use the DB libraries.
Once you sort out your library search path issue, you will then hit a problem with your connection string. You are trying to use an old SID construct, but should use a Service Name. It needs to be set like:
sn = 'xe'
dsn_tns = cx_Oracle.makedsn(host, port, service_name=sn)
The cx_Oracle documentation covers installation and also has a User Guide section that is worth reviewing https://cx-oracle.readthedocs.io/en/latest/index.html

Is it possible to use Windows DSN to connect to postgresql via sqlalchemy?

I have a connection to PostgreSQL created in the Windows ODBC Data Source. I'm using pyodbc to connect to the PostgreSQL database like this:
conn = pyodbc.connect('DSN=PostgreSQL35W')
I'm now using SQLAlchemy and would like to connect to the database using a Windows DSN like I do with pyodbc. Is this possible? I need to use the Windows DSN.

pyodbc fails to connect to database, but IBM data studio connects with same credentials

I'm trying to connect to a database on an IBM machine, and I can connect just fine via the IBM desktop client "IBM Data Studio." However, when I try to connect with pyodbc it fails to connect. I've received a series of errors but it seems the main response is along the lines of the following
pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
[TCP/IP Sockets]SQL Server does not exist or access denied. (17)
(SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionOpen (
Connect()). (10061); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
My code follows:
import pyodbc
# Specifying the ODBC driver, server name, database, etc. directly
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=*****; PORT=50000;DATABASE=****;UID=***;PWD=***')
# Create a cursor from the connection
cursor = cnxn.cursor()
cnxn.close()
I am working on a Windows machine.
You cannot use an ODBC Driver for Microsoft SQL Server, to connect to an instance of IBM DB2 (nor to any DBMS other than Microsoft SQL Server).
You need an ODBC Driver for IBM DB2 for this connection, such as those from my employer.
You could also use an ODBC-to-JDBC Bridge Driver, in combination with a JDBC Driver for IBM DB2, such as JTOpen (open source, from IBM).
Just because a jdbc connection from Data-Studio is working, it does not mean that your pyodbc will connect to Db2. pyodbc does not use jdbc, instead it will use the CLI/ODBC interface to Db2 which gets implemented by a suitable driver.
To use Db2 from pyodbc on Windows, ensure you first have either a Db2-client installed on Windows, or a Db2-server installed installed on Windows.
There are many kinds of Db2-clients. Much depends on the operating-system that runs your Db2-server (Z/OS, i-Series, Linux, Unix, Windows), and what kind of activities you want to perform on the client (developing, administering/monitoring, querying, or all three).
For some target platforms there are non-IBM drivers, but I won't discuss those.
In python, you can choose to connect to a database either with a DSN (Data Source Name) (usually this involves a shorter connection string)
or without a DSN (longer connection string containing all the details).
When learning, it may be easier to get Microsoft Windows to do most of the initial work with odbcad32.
This is most easy if the Db2-client is already configured to access one or more Db2-databases . The prereq is that the driver supports CLI/ODBC.
To define a DSN (either a system-DSN or a user-DSN) use the Microsoft odbcad32 tool to point to your Db2 database and verify connectivity.
If the Db2-server runs on Z/OS or i-Series, special licensing requirements may apply depending on whether you are directly connecting to the target Db2-server or whether you are using a Db2-connect gateway.
Take a note of the exact DRIVER string inside odbcad32 for the Db2-database, including case and spaces because you will need that in your python code.
For example, that driver name might look like 'IBM DB2ODBC DRIVER - DB2COPY1' if you have a local Db2-server installed on Windows (such as the free Db2-Express-C).
Verify that the connection to the Db2-database is successful inside odbcad32. That is crucial.
When odbcad32 succeeds to connect then pyodbc will succeed to connect usually.
In your python code, your connection-string can either use the DSN or explicitly quote the DRIVER/SERVER/PORT/DATABASE/UID/PWD, along with any other required settings on the connection string.
Remember also that you don't have to use pyodbc. There are other options for python to interact with Db2-Servers. Make an informed choice and do your research.
You can also use the IBM supplied module "ibm_db" or the DBI interface module "ibm_db_api", or if you are using an object relational mapper you can use the SQLAlchemy adapter (ibm_db_sa), or you can use django framework.
Read all about that in the Db2 documentation here.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.swg.im.dbclient.python.doc/doc/c0054366.html
I have solved the problem! The issue was that I was missing the "iSeries Access ODBC Driver". I don't think it's available online, although I could be wrong, I had to have our database guy help install it. Now I can connect through both the ibm_db and pyodbc libraries, after setting up my DSN under "odbcad32.exe." This took a while to solve but mostly because of the lack of informative documentation from IBM. Hopefully, this helps anyone in the same situation.

connecting to oracle timesten via cx_oracle in sqlalchemy

I can connect to oracle via cx_Oracle in sqlalchemy by this connection string:
connection_string = 'oracle+cx_oracle://user:pass#127.0.0.1/orcl'
and also i can connect TimesTen by cx_Oracle using this:
con = cx_Oracle.connect('user/pass#127.0.0.1/tt2:timestendirect')
but i don't know how to connect to TimesTen via cx_Oracle in sqlalchemy?
You should be able to create an entry in the TNSNAMES.ora configuration file and then use that directly. If you are using the instant client, you can set the environment variable TNS_ADMIN to point to a directory of your choosing that contains the tnsnames.ora configuration file.
https://docs.oracle.com/cd/E18283_01/timesten.112/e13066/oci.htm#BABHAAHJ
The easy connect syntax does not appear to be supported by sqlalchemy.

Categories

Resources