error when connecting oracle in python using cx_Oracle - python

I was trying to connect oracle database using python like below.
import cx_Oracle
conn = cx_Oracle.connect('user/password#host:port/database')
I've faced an error when connecting oracle.
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've been struggling to figure it out. I used my user name, password, host, port and database('orcl') for example,
'admin/admin#10.10.10.10:1010/orcl'.
Why coudn't it connect?
Ahh, btw I'm running all the code in azure notebooks.

That error indicates that you are missing a 64-bit Oracle client installation or it hasn't been configured correctly. Take a look at the link mentioned in the error message. It will give instructions on how to perform the Oracle client installation and configuration.
[Update on behalf of Anthony: his latest cx_Oracle release doesn't need Oracle Client libraries so you won't see the DPI-1047 error if you upgrade. The driver got renamed to python-oracledb but the API still supports the Python DB API 2.0 specification. See the homepage.]

This seems a problem with version 6.X.This problem didnot appeared in 5.X.But for my case a little workaround worked.I installed in my physical machine and only thing that i need to do was a pc reboot or reopen the terminal as i have added in the path of environment variables.You can try to install in physical machine instead using azure notebooks.

This error come when your Oracle Client is not installed or LD_LIBRARY_PATH is not set where libclntsh.so is present.
if you have Oracle client installed then search for libclntsh.so and set the LD_LIBRARY_PATH as
"export LD_LIBRARY_PATH=/app/bds/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2:$LD_LIBRARY_PATH"

Here is the full program to connect Oracle using python.
First, you need to install cx_Oracle. to install it fire the below command.
pip install cx_Oracle
import cx_Oracle
def get_databse_coonection():
try:
host='hostName'
port ='portnumber'
serviceName='sid of you database'
user = 'userName'
password = 'password'
dns = cx_Oracle.makedsn(host,port,service_name=serviceName)
con = cx_Oracle.connect(user, password, dns)
cursor = con.cursor()
query ="select * from table"
cursor.execute(query)
for c in cursor:
print(c)
except cx_Oracle.DatabaseError as e:
print("There is a problem with Oracle", e)
finally:
if cursor:
cursor.close()
if con:
con.close()
get_databse_coonection()

Related

How to connect MariaDB database on Synology NAS from SQLalchemy in python?

I would like to connect to my MariaDB10 database in a Synology NAS using python SQLalchemy. I installed PhpMyAdmin, and created a database named "test", and a random table called "company". I inserted a few rows of dummy data in the table already through the interface. Here is a snapshot of it.
My code is like this:
# Module Imports
import sqlalchemy
import pymysql
from sqlalchemy.ext.declarative import declarative_base
import config_maria_us
# Define the MariaDB engine using MariaDB Connector/Python
user = "username"
passwd = "password"
host = "192.168.1.111"
db = "test"
port= "3307"
engine = sqlalchemy.create_engine(f'mysql+pymysql://{user}:{passwd}#{host}:{port}/{db}')
sql_df = engine.execute("SELECT * FROM company" ).fetchall()
But this returns an error:
OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.111' ([Errno 61] Connection refused)")
Because of this page, so I keep using create_engine("mysql+pymysql:. It says to connect to a MariaDB database, no changes to the database URL are required.
I followed this page, and tried to install mariadb SQLAlchemy by brew install mariadb SQLAlchemy. But it shows a warning Warning: No available formula with the name "sqlalchemy". Did you mean sqlancer?
Then I ofcourse installed MariaDB Connector/C (by following this page) with brew install mariadb-connector-cand installed PyMySQL with pip install PyMySQL. Actually, to start with, i tried to installed mariadb with brew install mariadb, but after loading a pile of things, it shows failure,
Error: Cannot install mariadb because conflicting formulae are installed.
mysql: because mariadb, mysql, and percona install the same binaries
Please `brew unlink mysql` before continuing.
Unlinking removes a formula's symlinks from /opt/homebrew. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side effects in the
resulting software.
I did not go on installing it, because i don't know how to "relink" MySQL after the unlink.
That's pretty much it, would anyone please tell me what to do? by running the "engine = ..." syntax, it looks like i at least reached my server, but it still fail to connect as '(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server'
OP probably resolved by himself/herself but in case someone else still face the similar issue. In my case, after following steps, I can have access from python script to mariadb hosted in NAS.
Make sure MariaDB turn on TCP/IP Connection
Make sure your username from working machine IP has permission to the database. You can set this up by
GRANT ALL PRIVILEGES ON database_name.* TO 'username'#'localhost';

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

Superset cannot connect to my MSSQL database

After installing Superset (open source software from Airbnb) on my virtual machine (RHEL, Linux 7.2-11), I cannot add my MSSQL database in the configuration page.
Menu->Sources->Databases->Add
In the SQLAlchemy URI field, I entered :
mssql+pymssql://user:password#host:port/database
Obviously, with my user, password, host, port and database name.
But when I click "Test Connection" I have the following error :
ERROR: {"error": "Connection failed!
The error message returned was:
(pymssql.OperationalError) (18456, 'DB-Lib error message 20018, severity 14:\
General SQL Server error: Check messages from the SQL Server\
DB-Lib error message 20002, severity 9:\
Adaptive Server connection failed (host:port)')"}
I already installed the pymssql package and I do not really know where this error could come from.
I tried to follow these tutorials but, maybe I'm doing it wrong:
http://airbnb.io/superset/installation.html
http://airbnb.io/superset/tutorial.html#connecting-to-a-new-database
What worked for me is the following:
mssql+pymssql://user:pass#address.of.db/?charset=utf8
However, I didn't manage to define a database using this syntax, and in turn couldn't define/find the available tables.
I was also having problem to connect with MSSQL. I was on macOS Catalina. I took the following steps, and it worked:
brew install msodbcsql17 mssql-tools (see: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15)
pip install pyodbc
mssql+pyodbc://UserName:Password#HostIP,Port/DBName?driver=ODBC Driver 17 for SQL Server
Mentioning of 'driver=ODBC Driver 17 for SQL Server' in the end is important. (See: https://stackoverflow.com/a/51266453/13150101)
Please try mssql://user:password#host:port/database

How to use IBM_DB with older versions of DB2?

I want to connect Python to DB2 version 9.1 using IBM DB2 ODBC Driver.
Following is my code to connect Python to DB2. This program is working in the later versions of IBM DB2.
import ibm_db
conn = ibm_db.connect("DSN=PDB2;DRIVER={IBM DB2 ODBC DRIVER};DATABASE=MDBASIS;PORT=1234;PROTOCOL=TCPIP;UID=username;PWD=password","","")
stmt = ibm_db.exe_immediate(conn,"create table egg (ID SMALLINT, NAME VARCHAR(30))")
stmt = ibm_db.exe_immediate(conn,"insert into egg (ID, NAME) VALUES('1','ok')")
state = ibm_db.exe_immediate("select * from egg")
result = ibm_db.fetch_both(state)
while result != False:
print 'id = %d and name = %s' %(result[0],result[1])
result = ibm_db.fetch_both(state)
stmt = ibm_db.exe_immediate(conn,"drop table egg")
My problem is, the version of my IBM DB2 is 9.1 without FixPack and I get an error message when I try to connect to IBM DB2 9.1 version.
"[IBM][CLI Driver] CLI0133E Option type out of range. SQLSTATE=HY092 SQLCODE=-99999"
And the explanation for this error written in page http://programmingzen.com/2008/02/08/essential-guide-to-the-ruby-driver-for-db2/is:
"If you get this error, it usually means that you are using a version of DB2 that is too old. Install the latest FixPack or the latest version of DB2 (currently 9.5) to resolve the problem."
But I cannot install latest FixPack or the latest version of DB2 or in any way modify existing DB2 installation.
Question
Is there any way I can connect to DB2 version 9.1 without modifying the database, possibly using something else than IBM_DB?
I think the problem is in the client ibm_db driver. Basically you can connect to any older version from all the languages. Try to get another db2 driver. If I were you I would install the db2 client which comes with several drivers which are located in sqllib.
What version of ibm_db are you using? I am able to connect to DB2 9.1 with ibm_db 1.0.4 without problems.
Also: Based on the fact that you're specifying DSN in your connection string, I assume that you already have the database cataloged on the client as PDB2. You can greatly simply your connect statement to:
conn = ibm_db.connect('PDB2','username','password')
The only time you need to use the longer form (where you specify DATABASE/PORT/HOST/UID/PWD) is if you are using a DSN-less connection (i.e. the database has not been cataloged on the local machine).
I guess your DB2 client API version is higher than the DB2 server itself, ensure you use the same version on the client machine, try to connect and maintain your database remotely using the DB2 control center, and try to connect from python with:
conn = ibm_db.connect("MDBASIS", "username", "password")
AFAIK, this would force the wrapper to use native library instead of odbc.
ibm_db (and PyDB2 are just python modules that allow you to access the DB2 API calls through python. The DB2 queries themselves are being performed by your DB2 installation... something very separate from ibm_db and PyDB2.
It sounds like your DB2 installation is the problem, not ibm_db. You can test this by running db2 from the command line and seeing if you can execute your query directly in the db2 command line interface.

pyodbc and mySQL

I am unable to connect to mySQl db using pyodbc.
Here is a snippet of my script:
import pyodbc
import csv
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=mydb; UID=root; PASSWORD=thatwouldbetelling;")
crsr = cnxn.cursor()
with open('C:\\skunkworks\\archive\\data\\myfile.csv','r') as myfile:
rows = csv.reader(myfile, delimiter=',', quotechar='"')
for row in rows:
insert_str = 'INSERT into raw_data VALUES(something, something)'
print insert_str
#crsr.execute(insert_str)
cnxn.commit()
myfile.close()
I get this error at the pyodbc.connect() line:
pyodbc.Error: ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Data
source name not found and no default
driver specified (0)
(SQLDriverConnectW)')
I have another question regarding this error (and Python scripts in general). When I run this as a script, it fails silently (I was expecting a stack trace). I have to type each line in manually to find where the error occured.
I am being a bit lazy for now (no exception handling) - is this normal behaviour of a Python script without exception handling to fail silently?
[Edit]
I am not using mysqldb because I am already using pyodbc to extract my data from another source (MS Access). Ok, not a good reason - but I am already grappling with pyodbc and I dont really fancy having to wrestle with another library/module/package(whatever its called in Python) for a "one off" job. I just want to move my data of from various data sources in the Windows environment to mySQl on Linux. once on Linux, I'll be back on terra firma.
That is the entire 'script' right there. I just saved the code above into a file with a .py extension, and I run python myscript.py at the command line. I am running this on my XP machine
I had this same mistake so I went over all the version I was using for the connection. This is what I found out:
For Python 2.7 32 bits:
- pyodbc must be 32bits
- the DB Driver must be 32bits. (Microsoft Access should be 32 bits too)
For those who use the 64 bits version. You should check that everything is 64 bits too.
In my case I was trying to connecto to an Oracle DB and Microsoft Access DB so I had to make the following components match the architechture version:
pyodbc (MS Access)
python
cx_Oracle (Oracle dialect for SQLalchemy)
Oracle instantclient basic (Oracle. Do not forget to create the environment variable)
py2exe (Making the excecutable app)
Is that your driver name right?
You can check your driver name in
Windows -> Control panel -> System and security -> Administrative tools -> ODBC Data Sources -> Driver tab
then copy the river name to the first parameter
like
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 5.3 ANSI Driver}; SERVER=localhost;DATABASE=books; UID=root; PASSWORD=password;")
And my problem solved
or you may not install the driver and the step is simple.
MySQLdb (or oursql) and pyodbc both have the same interface (DB-API 2), only you don't have to deal with ODBC's issues if you use the former. I strongly recommend you consider using MySQLdb (or oursql) instead.
First, According to the official docs, if you want to connect without creating a DSN, you need to specify OPTION=3 in the connection string:
ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=venu;PASSWORD=venu;OPTION=3;"
If that fails to work, I'd further troubleshoot by creating a DSN.
Second, no Python should not be failing silently. If that is the case when you run your script, there is something else amiss.
only need install mysql-connector-odbc-3.51.28-win32.msi.
and pyodbc-2.1.7.win32-py2.7.exe.
of course, you have ready installed MySQL and python 2.7.
example:
import pyodbc
cndBase = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306;DATABASE=nameDBase; UID=root; PASSWORD=12345;")
ptdBase = cndBase.cursor()
query_str = 'SELECT* FROM nameTabla;'
rows = ptdBase.execute(query_str)
for rw in rows:
print list(rw)`enter code here`
I was getting the same error. It seemed the driver i was using to make the connection was not the driver installed in my system.
Type ODBC on windows run and select ODBC Data Source(32/64) based on where you have installed the driver.
From there click on System DSN and click add. From there you can see the MySQL driver installed in your system. Use the ANSI driver in your code where you are making the connection.

Categories

Resources