I am trying to connect to DB2 from Python on my Mac M1. Unfortunately, I have no control over the python version (Python 3.10.6) installed on the Mac. I tried to follow the instructions on Connect to DB2 via JayDeBeApi JDBC in Python to set up JDBC connection but failing with error
FileNotFoundError: [Errno 2] JVM DLL not found: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/lib/libjli.dylib
Though the file libjle.dylib exists in the above folder, the error pops up. I have tried trying to find a solution online with no luck.
I have connected to the same DB2 database through DBeaver on my Mac, but not able to through python.
Note, I don't have admin privileges to install/re-install but can only download packages from a repo.
Any pointers would really be very helpful
I tried the following code
import jaydebeapi
import jpype
jar = '/Users/JARFILES/jcc-11.5.7.0.jar' # location of the jdbc driver jar which is used by DBEaver
args='-Djava.class.path=%s' % jar
jvm = jpype.getDefaultJVMPath()
jpype.startJVM(jvm, args)
jaydebeapi.connect(
'com.ibm.db2.jcc.DB2Driver',
['jdbc:db2://server:port/database','myusername','mypassword']
)
Related
I am trying to connect a Firebird database with Python. I already tried it with pyodbc:
import os
import pyodbc
server = '127.0.0.1/3050'
database = 'Databse-Name'
username = 'Username'
password = 'password'
cnxn = pyodbc.connect('DRIVER={Firebird/InterBase(r)
driver};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
I get this error:
OperationalError: ('08004', "[08004] [ODBC Firebird Driver]Unable to connect to data source: library 'gds32.dll' failed to load (-904) (SQLDriverConnect); [08004] [ODBC Firebird Driver]Invalid connection string attribute (0)")
I am not sure why he tries to find 'gds32.dll'. In the ODBC-Connection I used this driver C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\fbclient.dll
I am using Firebird as a 64-bit version, so I am a bit clueless because of the 32 in 'gds32.dll'.
I am not sure, if it is the right way to try it with pyodbc. I am open for other advice.
Has anyone an idea why it is not working?
The fact the error mentions gds32.dll means it tried to load fbclient.dll, and that didn't work. Then it tried to fallback to gds32.dll. The gds32.dll is supported historically, because Firebird was forked from InterBase 22 years ago, and InterBase used the name gds32.dll for its client library. The 64-bit version is also called gds32.dll.
The problem is that, unless the C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ folder is explicitly on the path, or you configured the CLIENT connection property, that no library is found (or possibly it's 32-bit not 64-bit).
You need a 64-bit fbclient.dll. If that C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ is really a 64-bit Firebird (then C:\Program Files (x86) is the wrong location), you either need to specify the path of the 64-bit client library in the CLIENT connection property, or you can install it with - from a command prompt started as administrator - instclient i f from a Windows 64-bit Firebird installation, or do a client install using a Firebird installer. Alternatively, you can download the zipkit of a Windows 64-bit Firebird and use its fbclient.dll.
You should also consider using one of the Firebird drivers for Python, instead of using ODBC. You can choose from:
firebird-driver - uses fbclient.dll
FDB - uses fbclient.dll (deprecated and replaced by firebird-driver)
firebirdsql (aka pyfirebirdsql) - a pure Python driver (no native dependencies)
Also, I'm not sure if Gordon's advice about using SQLAlchemy is correct, but I'd recommend investigating that (though below the covers SQLAlchemy will probably use FDB or maybe firebird-driver, so you'd still need a proper 64-bit client library to load).
If you are going to use pandas with a database other than SQLite you should be using SQLAlchemy (ref: here). In your case you would use the sqlalchemy-firebird dialect.
Edit re: comment to original answer
Since we are connecting to localhost we can expect that Firebird has been installed and therefore the client tools are available (which is true for a default install). In that case, the following works on a Windows 8.1 test machine:
import pandas as pd
import sqlalchemy as sa
# note the r"" string
engine = sa.create_engine(r"firebird://SYSDBA:masterkey#localhost/C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB")
df = pd.read_sql_query("SELECT * FROM my_table", engine)
although a better approach would be to build the connection URL like this
connection_url = sa.engine.URL.create(
"firebird",
username="SYSDBA",
password="masterkey",
host="localhost",
database=r"C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB",
)
engine = sa.create_engine(connection_url)
I am trying to connect to my Oracle Database with Python.
I am currently using cx_Oracle to connect with it but doesnt work...
#!/usr/bin/python3
import cx_Oracle
dsn_tns = cx_Oracle.makedsn('Host Name', 'Port Number', service_name='Service Name')
conn = cx_Oracle.connect(user=r'user', password='password', dsn=dsn_tns)
c = conn.cursor()
c.execute('select * from person')
conn.close()
Error:
conn = cx_Oracle.connect(user=r'g_user', password='G_user', dsn=dsn_tns)
cx_Oracle.DatabaseError: 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 went on the Site and found nothing that could help...
Is something in my Code Wrong or is it something else ?
Make sure you have installed the correct oracle client (you can find the oracle client package from here "https://www.oracle.com/in/database/technologies/instant-client/winx64-64-downloads.html" and add the downloaded folder inside the folder where python is installed and then add this location (location of your client package folder) to system's environment variable. Hope that will work.
I suggest you to first check the compatibility of your OS, Python and Oracle Instant Client architecture:
import platform
platform.architecture()
Then, I definitely advise you to set the Oracle Instant Client inside your jupyter notebook:
import os
os.environ["PATH"] = "Complete Location of Instant Client Folder" + ";" + os.environ["PATH"]
Source: cx_Oracle error. DPI-1047: Cannot locate a 64-bit Oracle Client library
Your error is
DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory".
You need the Oracle Linux 64-bit client binaries in your operating system search path before you start the Python process.
The easiest way is to install the free Oracle Instant Client. Use the latest 19c version - it will let you connect to Oracle DB 11.2 or later. The install instructions are at the foot of the download page. If you install the RPMs, everything is configured for you. If you install the ZIPS I prefer setting the library search path with ldconfig which is also shown in the instructions.
I tried connecting my python server with IBM DB2 and got this error. I searched online, tried many things and nothing could fix it. I couldn't find the db2dsdriver.config anywhere in the IBM folder. For context, I'm trying to access an online IBM db using a python flask server that I'm running locally
File "server.py", line 8, in <module>
conn = ibm_db.connect("BLUDB","MyDB2loginhere","psswd")
Exception: [IBM][CLI Driver] SQL1531N The connection failed because the name specified with the DSN connection string keyword could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file. Data s SQLCODE=-1531cified in the connection string: "BLUDB"
Answered by comments.
By default when you install python module ibm_db it delivers a small ODBC driver from IBM called clidriver for connecting to local or remote Db2 databases. Other ODBC drivers are available, including from other suppliers different from IBM. With an IBM supplied driver (including clidriver), an additional license may be required only for accessing Db2-for-Z/OS, or Db2 for i series. Specifically for i-series, it is cheaper to use the ODBC driver option that is available via IBM product called IBM i access (cheaper than buying a license for Db2-connect to use with clidriver).
To connect to a database, you either use an externally configured DSN (data source name), or use a connection string in your code.
Externally configured DSNs mean that your code does not hard-code any connectivity details, so it is easier to move between different environments without changing code, because the configuration is all external data. You can also arrange the connection-strings to be external.
To configure an external DSN, either use local tools or edit the db2dsdriver.cfg file, which is a small XML file you can create and edit manually or via db2cli command lines. Local tools can be odbcad32 on MS-Windows or unixODBC on Linux/Unix/MacOS. The IBM published documentation in the Db2 Knowledge Center gives all the details of db2dsdriver.cfg.
To use a connection-string, you need the target database hostname (server name, or cloud service name), the port-number, the database-name, and a userid+password (or personal-certificate for Db2-for-Z/OS). The format of the connection string is a semi-colon separated/terminated collection of X=Y pairs, like this:
conn_string="Server=hostname_or_ip_address_of_the_Db2_server_Or_Cloud_Service;Port=50000;Database=BLUDB;UID=***;PWD=***;"
Many other additional and optional settings can be set in that connection string to control other aspects of the connection, or to specify encryption. These are connection attributes or connection properties and are all documented in the Db2 knowledge centre.
The python code to use the connection string looks like:
conn = ibm_db.connect( conn_string , "", "" )
or
conn = ibm_db.connect( conn_string, user, password)
if you exclude the UID and PWD from the connection string and supply then as the second and third parameters to the ibm_db.connect() .
How I managed to do in 2021. What you will need:
Python 3.7
PipEnv
Ibm-db
Ibm-db version is not important but this lib only works with Python 3.7 (current python version is 3.9).
Install Python 3.7.6 in your machine (this is the version that worked).
In your IDE create a new python file.
Let's create a Virtual Enviroment to make sure we will use Python 3.7
pip install pipenv
After installing
pipenv install --python 3.7
Activate the Virtual Environment
pipenv shell
You can use pip list to verify if you are in the new Virtual Enviroment - if list only shows 3 or 4 libs, it's because you are
Now you can download Ibm_db
pip install ibm-db
You may add this to your code to confirm what is the version you are using
from platform import python_version
print(python_version())
Now accessing the DB2
import ibm_db_dbi as db
# Connect to DB2B1 (keep Protocol as TCPIP)
conn = db.connect("DATABASE=DBNAME;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=Your User;PWD=Your Password;", "", "")
Checking all tables available
for t in conn.tables():
print(t)
Your SQL code:
sql_for_df = """SELECT *
FROM TABLE
WHERE ..."""
Visualizing as DataFrame
First install pandas as it will not be present in your Virtual Environment
pip install pandas
After that import to your code and play around
import pandas as pd
df = pd.read_sql(sql_for_df, conn)
df.head()
To exit the VIrtual Enviroment just write exit in your terminal. If you want to remove the Virtual Enviroment write in the terminal pipenv --rm
That's pretty much all I could learn so far. I hope it helps you all.
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.
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