Home Backend Development Python Tutorial Python connects to DB2 database

Python connects to DB2 database

Feb 25, 2017 am 09:54 AM

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:

Python connects to DB2 database

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)

Python connects to DB2 database

Enter? Just press Enter and it will be displayed Command list, open the database manager:

Python connects to DB2 database

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.

Python connects to DB2 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.

Python connects to DB2 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

Python connects to DB2 database

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()
Copy after login

Connect to the database and set up automatic submission

Query

sql = “select * from testable”

result = cursor.execute(sql)
Copy after login

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()
Copy after login

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)
Copy after login

update

sql = “update \”MYSCHEMA\”.\”TESTTABLE \” set \”content\” = ‘%s' where \”uuid\” = ‘%s'” % (

“aaa”, “1234567890”)

result = cursor.execute(sql)
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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 in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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)...

See all articles