Python connects to DB2 database
DB2 is a relational database management system developed by IBM in the United States. Its main operating environments are UNIX (including IBM's own AIX), Linux, IBM i (formerly known as OS/400), z/OS, and Windows Server version. Today we will discuss how to use Python to connect to DB2 database
I encountered such a situation at work. The project needs to connect to IBM's relational database (DB2). There are relatively few libraries in this area, among whichibm_db is a relatively easy-to-use library. There are also tutorials on the Internet, but they seem to be inaccurate, not very detailed, and full of errors. I have no choice but to get it and analyze the source code myself, and finally get it.
Installation
Environmental requirements:
First is the database DB2, download the connection directly to Baidu, I downloaded these two File:
Just download the one pointed by the arrow. I haven’t tested it on Linux yet.
Database API (I have been looking for this thing for a long time, and finally found the right one) (Cannot find search: SQLAPI.zip)
Python2.7
VCForPython2.7
ibm_db (the main library, the ntx64_odbc_cli library will be downloaded during installation, and the IBM_DB_HOME variable will be detected during installation, so you need to install the database before installing ibm_db)
The above modules are in It can be found online, please download and install it yourself.
Building a database
After the database is installed, create a new instance. The default is DB2, and then create a new database. The MYTEST I created (in the operation database and link database Note the case), command line method:
Open the command line processor: (administrator identity)
Enter? Just press Enter and it will be displayed Command list, open the database manager:
Then just close it. It is more convenient to use db2 data studio to establish the database and create tables. Create a temporary in the root directory during installation directory, unzip the file, and then modify the properties of install.exe to be compatible with Windows 7 and open it with administrator rights. After installation, click on the left to create a new database.
Fill in the above method. The username and password should be the username and password set when installing the database.
After the instance is configured and tested successfully, you can create the database.
Just write down the database name and alias, leave out the rest because it is for testing, and wait for the formal environment to examine the configuration for performance optimization. Click Run to create. The process is a bit slow. I don’t know if it is due to the machine configuration. It took about ten minutes.
The process of creating a table will not be described in detail below. It is important to note that before creating a table, you must first create a schema and use a custom schema to create the table.
Connection
Connect the direct import library
Just import ibm_db_dbi.
import ibm_db_dbi conn = ibm_db_dbi.connect(“PORT=50000;PROTOCOL=TCPIP;”, host=db[“host”], database=db[“database”], user=db[“user”], password=db[“passwd”]) conn.set_autocommit(True) cursor = conn.cursor()
Connect to the database and set up automatic submission
Query
sql = “select * from testable” result = cursor.execute(sql)
Note that the above query method is wrong. The correct answer is as follows:
sql = “select * from MYSCHEMA.TESTTABLE” result = cursor.execute(sql) rows = cursor.fetchall()
The operation here is no different from MySQL
This place has been fooled for several hours , T_T
insert
sql = “insert into MYSCHEMA.TESTTABLE (“uuid”, “content”) values (‘%s', %s)” % (“1234567890”, “asdfghjkl”) result = cursor.execute(sql)
update
sql = “update \”MYSCHEMA\”.\”TESTTABLE \” set \”content\” = ‘%s' where \”uuid\” = ‘%s'” % ( “aaa”, “1234567890”) result = cursor.execute(sql)
If the operation is successful, the result is True. Pay attention to the quotation marks of each statement. The odd and even numbers must be in the above way.
The above is the entire content of using Python to connect to the DB2 database shared in this article. I hope it can be helpful to my friends.
For more articles related to Python connecting to DB2 database, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
