Introduction to IBM Db2
IBM Db2, a family of data management products, is designed to store, analyze, and retrieve the data efficiently and securely. It is widely used across industries for its reliability and excellent performance in handling data.
Db2 API Examples
Connecting to the Db2 Database
Before working with Db2, you need to establish a connection to the database. Here’s a simple example showing how to connect:
import ibm_db conn = ibm_db.connect("DATABASE=db_name;HOSTNAME=hostname;PORT=50000;PROTOCOL=TCPIP;UID=user;PWD=password;", "", "") if conn: print("Connected to the database.") else: print("Failed to connect to the database.")
Executing a Simple Query
Once connected, you can execute SQL queries. Here is how you can execute a simple SELECT query:
query = "SELECT column1, column2 FROM table_name" stmt = ibm_db.exec_immediate(conn, query) result = ibm_db.fetch_both(stmt) while result: print(result) result = ibm_db.fetch_both(stmt)
Inserting Data into Db2
Inserting data into a Db2 table is straightforward. Here’s an example:
insert_query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')" stmt = ibm_db.exec_immediate(conn, insert_query) if stmt: print("Data inserted successfully.") else: print("Data insertion failed.")
Updating Data in Db2
Updating existing records in the database can be done with the UPDATE statement. Example:
update_query = "UPDATE table_name SET column1 = 'new_value1' WHERE column2 = 'value2'" stmt = ibm_db.exec_immediate(conn, update_query) if stmt: print("Data updated successfully.") else: print("Data update failed.")
Deleting Data from Db2
To delete records from a table, you use the DELETE statement:
delete_query = "DELETE FROM table_name WHERE column2 = 'value2'" stmt = ibm_db.exec_immediate(conn, delete_query) if stmt: print("Data deleted successfully.") else: print("Data deletion failed.")
Example Application
Let’s put everything together in a simple example application that demonstrates connecting to a Db2 database, performing CRUD operations, and disconnecting from the database:
import ibm_db def connect_db(): conn = ibm_db.connect("DATABASE=db_name;HOSTNAME=hostname;PORT=50000;PROTOCOL=TCPIP;UID=user;PWD=password;", "", "") if conn: print("Connected to the database.") else: print("Failed to connect to the database.") return conn def execute_query(conn, query): stmt = ibm_db.exec_immediate(conn, query) return stmt def close_db(conn): ibm_db.close(conn) print("Database connection closed.") def main(): conn = connect_db() print("Executing SELECT query:") query = "SELECT column1, column2 FROM table_name" stmt = execute_query(conn, query) result = ibm_db.fetch_both(stmt) while result: print(result) result = ibm_db.fetch_both(stmt) print("Executing INSERT query:") insert_query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')" execute_query(conn, insert_query) print("Executing UPDATE query:") update_query = "UPDATE table_name SET column1 = 'new_value1' WHERE column2 = 'value2'" execute_query(conn, update_query) print("Executing DELETE query:") delete_query = "DELETE FROM table_name WHERE column2 = 'value2'" execute_query(conn, delete_query) close_db(conn) if __name__ == "__main__": main()
This example covers some of the fundamental operations you can perform using Db2’s API, showcasing the ease of interaction with a Db2 database.
Hash: 00213a714f4f50b7933265b2f406cc47e4b39da7aa5273d36348ef9578771a4b