I am trying to connect DB2 with pyodbc on linux and I get the error
"('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'IBM DB2 ODBC Driver' : file not found (0) (SQLDriverConnect)")## Heading ##
Is there some installation required or am I missing path?
# Create a connection object
cnxn = pyodbc.connect(
'Driver={IBM DB2 ODBC Driver}; '
'Hostname=nn.nn.nnn.nnn; '
'Port=50000; '
'Protocol=TCPIP; '
'Database=myDB; '
'CurrentSchema=myschema; '
'UID=user1; '
'PWD =test1;'
)
I tried to configure by following the link at https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/t0061216.html
I did a
yum install unixODBC
But I could not find the path where libdb2.so was installed for configuring
odbcinst.int as given here
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/t0061216.html
What is the path for libdb2.so?
The driver-manager (unixODBC) does not supply a driver for the Db2 rdbms. Normally you will use a Db2-driver supplied by IBM which works with unixODBC.
If your Db2-server is remote from Linux then you need to install Db2 driver separately and configure it.
If your Db2-server is already installed locally on Linux, then it includes a local Db2-client already, and libraries are symlinked from the Db2-instance home directory sqllib/lib64 directory ( for example /home/db2inst1/sqllib/lib64 will contain the symlinks to the relevant libraries, if your Db2-instance is called "db2inst1"). In this case you don't need to install anything else.
Otherwise, there are various IBM supplied Db2-clients, so you have to choose the right one, which depends both on your Db2-server platform and your programming needs (different footprints for different drivers). There are also third party drivers, not mentioned here. See here for Db2 client types:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.5.0/com.ibm.swg.im.dbclient.install.doc/doc/c0022612.html
[ 1 ] If your remote Db2-server runs on Linux/Unix/Windows/zLinux then download your IBM driver from Passport Advantage (choose the latest driver version for your Db2-server version). Your Db2 DBA (if you have one) should also have an installable copy although it's always wise to download the latest version from Passport Advantage website as long as you have that access.
If your Db2-server runs on Z/OS or i-Series AND your company already has a Db2-connect gateway server installed and configured then you can use the same driver as [1] above. The Db2-connect gateway is a separate hostname that does the protocol conversion (if necessary) and allows easier licensing at a price.
If your remote Db2-server runs on i-Series (formerly OS/400) and your company is not using a Db2-connect gateway, ask your i Admin for the correct driver and install that on Linux. For i-Series you can either use a driver from IBM or from a third party, as long as it supports ODBC/CLI on Linux for your version and bitness.
If your Db2-server runs on Z/OS and your company does not use a Db2-connect gateway (i.e. instead you are connecting directly from Linux to Db2-for-Z/OS) then you will need to use the same driver as in [1] but additionally apply a suitable license file to your Linux Db2 driver. Your admin should know the details.
Related
When trying to make a program on Windows that connects to a database via ODBC, I got the following error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.
I'm sure my code is right. It even worked on a different PC.
Why am I getting this error? And How do I fix it?
What causes this error?
The error message tells you, simply put, that the ODBC Driver Manager could not find the driver you specified in your connection string or DSN.
This can have 3 common causes:
The driver you tried to use is not installed on your system
The driver is installed, however, it doesn't match bitness of the code you're running
You made an error in typing the driver name
How do I check which drivers are installed on my system?
You can check the drivers which are installed on your system by going to the ODBC Data Source Administrator. To open it, press ⊞ Win + R, and type in: odbcad32.exe. Then check the tab Drivers for installed drivers. The Name column indicates the exact name you should use in your connection string or DSN.
If you're on 64-bit Windows, that only lists the 64-bit drivers installed on
your system. To see which 32-bit drivers are installed, press press ⊞ Win + R, and type in: C:\Windows\SysWOW64\odbcad32.exe, and go to the Drivers tab again.
The driver is installed, but it might be the wrong bitness, what do I do?
Then, you have two choices, either adjust the bitness your program is running in, or install a driver with a different bitness.
Some of the drivers that are installed by default on Windows only have a 32-bits variant. These can't be used with 64-bits programs.
You can usually identify which bitness a program is running under in task manager. In Windows 10, all 32-bit programs have (32-bit) appended to their name. If that isn't there, you're likely running a 64-bit program, and most modern programming languages and environments run on 64-bit by default, but allow you to switch to 32-bit. However, the specifics for different programming languages are outside the scope of this question.
How can I verify I didn't mistype the driver name?
An ODBC connection string looks like this:
DRIVER={DriverName};ParameterName1=ParameterValue1;ParameterNameN=ParameterValueN;
The driver name part needs to be delimited by curly braces if it might contain special characters, and needs to exactly match the installed driver name, as found in the ODBC Data Source Administrator, including spaces and typographical characters, but excluding capitalization.
Note that for deployed code, the driver must be present on the computer/server running the code.
I don't have the driver, or have the wrong bitness, where do I get the right one?
That depends on which driver you want to use.
A list of common drivers with download locations (all 32-bit and 64-bit at the same URL):
The Microsoft ODBC Driver 17 for SQL Server
The Microsoft Access database driver, which is part of the Microsoft Access Database Engine. Note that simultaneous installations of 32-bit and 64-bit Access ODBC drivers are not supported.
The MySQL ODBC connector by Oracle
The open-source SQLite ODBC driver by Christian Werner (non-official)
psqlODBC, the official PostgreSQL driver
If the driver you want to use isn't listed, the location is usually easily found using Google.
In design mode, a value has been set to the property of
TFDConnection.ConnectionDefName must be empty.
When trying to make a program on Windows that connects to a database via ODBC, I got the following error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.
I'm sure my code is right. It even worked on a different PC.
Why am I getting this error? And How do I fix it?
What causes this error?
The error message tells you, simply put, that the ODBC Driver Manager could not find the driver you specified in your connection string or DSN.
This can have 3 common causes:
The driver you tried to use is not installed on your system
The driver is installed, however, it doesn't match bitness of the code you're running
You made an error in typing the driver name
How do I check which drivers are installed on my system?
You can check the drivers which are installed on your system by going to the ODBC Data Source Administrator. To open it, press ⊞ Win + R, and type in: odbcad32.exe. Then check the tab Drivers for installed drivers. The Name column indicates the exact name you should use in your connection string or DSN.
If you're on 64-bit Windows, that only lists the 64-bit drivers installed on
your system. To see which 32-bit drivers are installed, press press ⊞ Win + R, and type in: C:\Windows\SysWOW64\odbcad32.exe, and go to the Drivers tab again.
The driver is installed, but it might be the wrong bitness, what do I do?
Then, you have two choices, either adjust the bitness your program is running in, or install a driver with a different bitness.
Some of the drivers that are installed by default on Windows only have a 32-bits variant. These can't be used with 64-bits programs.
You can usually identify which bitness a program is running under in task manager. In Windows 10, all 32-bit programs have (32-bit) appended to their name. If that isn't there, you're likely running a 64-bit program, and most modern programming languages and environments run on 64-bit by default, but allow you to switch to 32-bit. However, the specifics for different programming languages are outside the scope of this question.
How can I verify I didn't mistype the driver name?
An ODBC connection string looks like this:
DRIVER={DriverName};ParameterName1=ParameterValue1;ParameterNameN=ParameterValueN;
The driver name part needs to be delimited by curly braces if it might contain special characters, and needs to exactly match the installed driver name, as found in the ODBC Data Source Administrator, including spaces and typographical characters, but excluding capitalization.
Note that for deployed code, the driver must be present on the computer/server running the code.
I don't have the driver, or have the wrong bitness, where do I get the right one?
That depends on which driver you want to use.
A list of common drivers with download locations (all 32-bit and 64-bit at the same URL):
The Microsoft ODBC Driver 17 for SQL Server
The Microsoft Access database driver, which is part of the Microsoft Access Database Engine. Note that simultaneous installations of 32-bit and 64-bit Access ODBC drivers are not supported.
The MySQL ODBC connector by Oracle
The open-source SQLite ODBC driver by Christian Werner (non-official)
psqlODBC, the official PostgreSQL driver
If the driver you want to use isn't listed, the location is usually easily found using Google.
In design mode, a value has been set to the property of
TFDConnection.ConnectionDefName must be empty.
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.
Hello I'm getting this error while trying to save a Pandas dataframe into the ibm db2 database:
Can't load plugin: sqlalchemy.dialects:ibm_db_sa
I tried this solution but is does not work:
df = pd.read_csv('https://data.cityofchicago.org/resource/jcxq-k9xf.csv')
engine = sqlalchemy.create_engine('ibm_db_sa://'+ dsn_uid + ':' + dsn_pwd + '#'+dsn_hostname+':'+dsn_port+'/' + dsn_database )
chicago_socioeconomic_dataSQL = df.to_sql('chicago_socioeconomic_data', engine, if_exists = 'append', index=False)
Anybody can help me?
Thank you,
Matteo
Resolved by installing the required pre-requisite module (ibm_db_sa ) which will also install the ibm_db module and the ibm_db_dbi module, and (by default, unless otherwise directed) will also install the Db2 ODBC and CLI driver into your site_packages tree.
IF you are connecting to Db2-for-Linux/Unix/Windows, you do not need to install other IBM software or licence files.
If you are connecting directly to Db2-for-i-series (AS/400) , from either Microsoft-Windows, or Linux, then you should first install 'IBM i access' with the optional ODBC/CLI support, and get it configured to connect to your Db2 for i. Refer to its documentation for details. Set the IBM_DB_HOME environment variable to point to the directory where the 'IBM i access' components are installed. It is this environment variable that will tell the ibm_db module to not install the regular ODBC/CLI driver and to use your IBM i access components instead. Then you can install the ibm_db_sa module which will then use your 'IBM i access' product to communicate with the i-series database.
If you are connecting directly to Db2 for Z/OS, you will be unable to connect without a licence file for Db2-connect, OR you can connect indirectly via a separate Db2-connect gateway (in which case you do not then need to deploy a separate licence file). Refer to IBM's instructions for deploying the licence file to the correct location where the clidriver can use it.
I host a web app on Windows Azure (Flask), which runs on Windows Server.
I have a linux development environment (some other contributors may use windows in the future), so we use pyodbc to communicate with the SQL Server. Unfortunately, I have to change my connection string every time I pull or push from the Azure deployment due to linux and windows ODBC connection differences.
Consider my connection string:
pyodbc.connect('Driver=SQL
Server;Server=tcp:mydbname.database.windows.net,1433;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
This works fine once it's deployed on the server (Windows Server), or on the windows box, but doesn't work on the linux dev box. I found some over SO questions and a guide on how to setup FreeTDS (e.g. Connecting to Microsoft SQL Server through pyODBC on Ubuntu and http://www.gazoakley.com/content/connecting-sql-azure-python-ubuntu-using-freetds-and-unixodbc, http://blog.tryolabs.com/2012/06/25/connecting-sql-server-database-python-under-ubuntu/).
However, after following those guides through you have to remove the server name and replace it with a the DSN (data source name), which is a locally configured variable. While that will work on a local linux box, that doesn't work once it's deployed to the Azure web server (windows).
pyodbc.connect('DSN=SQL Server;Driver=SQL
Server;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
You have to remove the servername otherwise it conflicts with the odbc/FreeTDS config.
With no DNS specified (first connection string in this post):
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source
name not found, and no default driver specified (0) (SQLDriverConnect)
With DNS and servername:
pyodbc.Error: ('HY000', '[HY000] [unixODBC][FreeTDS][SQL Server]Only
one between SERVER, SERVERNAME and DSN can be specified (0)
(SQLDriverConnect)')
With DNS and no server name
When deployed to the server it wont work.
We use git for version control, and I don't want to have continuous merge conflicts as developers change their connection string to match their dev enviroments and/or deploy to the Azure site.
Is there a clean fix for this. Gut tells me I'm doing something wrong, and this is very trivial.
First of all, if you have successfully connected to SQL Server both in Windows and Linux platforms, congratulations. In python, there is a package installed by python default named ‘platform’, we can use it to check the what platform python script run in. And we can set 2 connection string in advance, and select the specific string in different platform.
Here is my python code snippet:
import platform
def getOBCDString():
stsos = platform.system()
bol = False
odbcstring=''
if(stsos == "Windows"):
bol = True
odbcstring = 'windows_odbc_string'
elif(stsos == "Linux"):
bol = True
odbcstring = 'Linux_odbc_string'
#else:
# custom error handle
return (bol,odbcstring)
bol,string = getOBCDString()
if(bol):
print(string)