Home > Backend Development > Python Tutorial > Python connects to DB2 database

Python connects to DB2 database

高洛峰
Release: 2017-02-25 09:54:28
Original
3923 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template