Using Python to Operate IBM DB2

To operate an IBM DB2 database, we can use Python's' IBM '_ DB ` package. This package provides the ability to connect and perform operations with a DB2 database. Firstly, we need to install 'IBM'_ DB ` package. You can install using the following command: bash pip install ibm_db After the installation is completed, we can use the following code samples to connect and operate the IBM DB2 database: python import ibm_db #Connect to a DB2 database conn = ibm_db.connect( "DATABASE=name;HOSTNAME=hostname;PORT=port;PROTOCOL=protocol;UID=username;PWD=password;", "", "" ) #Insert Data def insert_data(): try: #Create an SQL statement stmt = ibm_db.prepare(conn, "INSERT INTO employees (id, name) VALUES (?, ?)") #Binding parameters id = 1 name = "John" ibm_db.bind_param(stmt, 1, id) ibm_db.bind_param(stmt, 2, name) #Execute SQL statements ibm_db.execute(stmt) Print ("Data insertion successful!") except Exception as e: Print ("Data insertion failed:", e) #Query data def select_data(): try: #Create an SQL statement stmt = ibm_db.prepare(conn, "SELECT * FROM employees") #Execute SQL statements ibm_db.execute(stmt) #Get Result Set result = ibm_db.fetch_both(stmt) while result: #Output Results Print ("ID:", result [0], "Name:", result [1]) result = ibm_db.fetch_both(stmt) except Exception as e: Print ("Failed to query data:", e) #Update data def update_data(): try: #Create an SQL statement stmt = ibm_db.prepare(conn, "UPDATE employees SET name = ? WHERE id = ?") #Binding parameters name = "David" id = 1 ibm_db.bind_param(stmt, 1, name) ibm_db.bind_param(stmt, 2, id) #Execute SQL statements ibm_db.execute(stmt) Print ("Data update successful!") except Exception as e: Print ("Data update failed:", e) #Delete data def delete_data(): try: #Create an SQL statement stmt = ibm_db.prepare(conn, "DELETE FROM employees WHERE id = ?") #Binding parameters id = 1 ibm_db.bind_param(stmt, 1, id) #Execute SQL statements ibm_db.execute(stmt) Print ("Data deleted successfully!") except Exception as e: Print ("Data deletion failed:", e) #Insert Data Example insert_data() #Query Data Example select_data() #Update Data Example update_data() #Delete Data Example delete_data() #Close database connection ibm_db.close(conn) Attention: -Based on your actual situation, replace the parameters for connecting to the database (name, hostname, port, protocol, username, password). -Based on your actual situation, replace table names, column names, and SQL statements. -The above example only demonstrates the basic insert, query, update, and delete operations, and the specific SQL statements and table structure need to be adjusted according to the actual database design. I hope the above examples can help you!