error trying to access IBM DB2 using python - python

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.

Related

How to locate Oracle Instant Client in Python in Docker CI/CD pipeline

I am trying to connect to oracle DB to execute some SQL queries and fetch data through a python script . I have imported cx_Oracle and tried connecting.I got the error as - Exception - 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 was raised.
I downloaded instaclient and used that in my script and it worked using the below commands :
LOCATION = r"C:\instantclient_19_5"
os.environ["PATH"] = LOCATION + ";" + os.environ["PATH"]
But now I need to use this in CI CD pipeline. I have created a docker image for instaclient and python I am trying use this into my script. But I am not sure how to use add instaclient location in script (like the above code snippet) Could you please help me with this.
If you were deploying to Windows (or macOS), you could have used the new cx_Oracle 8 init_oracle_client() function, which is preferred to fiddling with PATH.
However it seems you are deploying to Linux, meaning the system library search path needs to contain all library directories before the process start. So you need to use ldconfig or set LD_LIBRARY_PATH as traditionally used on Linux. The cx_Oracle doc Installing cx_Oracle on Linux and Locating the Oracle Client Libraries covers this.
Also see sample docker images for Linux developers and for basic Oracle Instant Client. If you are not on an RPM based system, check out the sample Docker files in Docker for Oracle Database Applications in Node.js and Python.
In summary, download Oracle Instant Client Basic or Basic Light packages from here. If you got the ZIP files then run something like:
RUN echo /opt/oracle/instantclient_19_8 > /etc/ld.so.conf.d/oic.conf && \
ldconfig
The details vary with what base Docker image you are using, and whether you want to install Instant Client from ZIP files or RPMs.

Error while trying to connect to ibm db2 database through pandas: Can't load plugin: sqlalchemy.dialects:ibm_db_sa

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.

Connecting Python 3.x to Oracle DB

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.

Python and Oracle DB - "Error DPI-1050: Oracle Client library must be at version 11.2 or higher"

Some days ago, I was asked to develop a Python application capable of connecting to a Oracle DB. Since I already have an Oracle client installed (version 12.2.0), I just pip installed cx_Oracle and tried to establish a connection using below code:
import pandas as pd
import cx_Oracle
connection = cx_Oracle.connect('username/password#service_as_described_in_tnsnames.ora')
cur=connection.cursor()
input("Press Enter to continue...")
cur.execute('select* from MY_PRETTY_TABLE')
for line in cur:
print()
cur.close()
connection.close()
But when trying to run it, I got the error "DPI-1050: Oracle Client library must be at version 11.2 or higher". After googling it, I found this answer, and tried to change my code to:
my_dsn = cx_Oracle.makedsn("host",port,sid="sid")
connection = cx_Oracle.connect(user="user", password="password", dsn=my_dsn)
cursor = connection.cursor()
querystring = "SQL query"
cursor.execute(querystring)
But still, same error. It's important to notice that I have already used Oracle DB client in this same machine, to connect a DB with Power BI.
Also, if it can be helpful, my paths are setted as:
C:\instantclient_12_1
C:\Users\oracle2\product\12.1.0\client_1
C:\Users\oracle2\product\12.1.0\client_1\bin
C:\Users\oracle\product\12.2.0\dbhome_1\bin
That error implies that you have another older version of the Oracle client installed somewhere earlier in your PATH. You should do a search for OCI.DLL on your machine (using where.exe or the dir command) and either move or remove any unnecessary copies or adjust PATH as needed. Some older applications stuffed OCI.DLL in C:\Windows\System32 improperly, for example.
This problem had to do with Oracle Instant client Version 19.3.0.0.0.
I uninstalled it and installed the previous version Oracle Instant client 12.2.0.1.0 to and it worked.
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Important things to do before you try above:
From Windows command prompt
c:> where oci.dll
make sure you find only one entry and remove the redundant ones.
Ensure you set the environment path to the newly installed client.

cx_Oracle.DatabaseError: ORA-12541: TNS:no listener

I want to learn how to work with an Oracle Database using Python. If I understand correctly, you can use Oracle Instant Client to connect to an Oracle Database remotely, but I cannot connect. I suspect the issue is that I don't know what arguments to use for the localhost/instance combo. I believe localhost is simply my machine name, or it may be literally 'localhost' which I have tried, but I don't know how and can't find help to tell me how to locate the service name for the database instance.
In a prompt I opened python, imported cx_Oracle and used 'Easy Connect String' as specified in this sample code, using either "(my machine name)/orclpdb" or literally "localhost/orclpdb" for DEFAULT_CONNECT_STRING.
The sample code creates a variable MAIN_CONNECT_STRING which I used in a command prompt to attempt to connect to the remote database:
cnxn = cx_Oracle.connect(MAIN_CONNECT_STRING)
cx_Oracle.DatabaseError: ORA-12541: TNS:no listener
I find answers that seem to be based on this, or others referring to tnsnames.ora, or listener.ora which are files I don't have. I also tried using instantclient-sqlplus-nt-12.2.0.1.0.
Background:
Windows 7
I downloaded cx_Oracle-6.1-cp27-cp27m-win32.whl and
instantclient-basic-nt-12.2.0.1.0.zip
I put the .whl into "C:\Python27\Scripts\"
I used python -m pip
install cx_Oracle --upgrade to install cx_Oracle.
I unzipped the instant client zip and put the child folder here
"C:\instantclient_12_2"
I added ;C:\instantclient_12_2 to the end of PATH.
Maybe you can use the installation instructions found in the
cx_Oracle documentation for Windows, cx_Oracle for Windows
Uninstall first the cx_Oracle you are using then try to follow the instructions found in the link above.

Categories