1 00:00:00,000 --> 00:00:06,510 Hello, and welcome to connecting to a database using the ibm_db API. 2 00:00:06,510 --> 00:00:08,420 After completing this lesson, 3 00:00:08,420 --> 00:00:12,555 you will be able to understand the ibm_db API, 4 00:00:12,555 --> 00:00:16,785 as well as the credentials required to connect to a database using Python. 5 00:00:16,785 --> 00:00:19,020 We will also demonstrate how to connect to 6 00:00:19,020 --> 00:00:25,165 an IBM DB2 database using Python code written on a Jupyter notebook. 7 00:00:25,165 --> 00:00:29,180 The ibm_db API provides a variety of 8 00:00:29,180 --> 00:00:31,790 useful Python functions for accessing and 9 00:00:31,790 --> 00:00:35,285 manipulating data in an IBM data server database, 10 00:00:35,285 --> 00:00:38,020 including functions for connecting to a database, 11 00:00:38,020 --> 00:00:40,490 preparing and issuing SQL statements, 12 00:00:40,490 --> 00:00:44,285 fetching rows from result sets, calling stored procedures, 13 00:00:44,285 --> 00:00:46,650 committing and rolling back transactions, 14 00:00:46,650 --> 00:00:49,690 handling errors and retrieving metadata. 15 00:00:49,690 --> 00:00:56,310 The ibm_db API uses the IBM Data Server Driver for ODBC, 16 00:00:56,310 --> 00:01:01,445 and CLI APIs to connect to IBM, DB2, and Informix. 17 00:01:01,445 --> 00:01:06,470 We import the ibm_db library into our Python application. 18 00:01:06,470 --> 00:01:09,200 Connecting to the DB2 requires 19 00:01:09,200 --> 00:01:13,240 the following information: a driver name, a database name, 20 00:01:13,240 --> 00:01:17,170 a host DNS name or IP address, a host port, 21 00:01:17,170 --> 00:01:20,170 a connection protocol, a user ID, 22 00:01:20,170 --> 00:01:22,015 and a user password. 23 00:01:22,015 --> 00:01:27,120 Here is an example of creating a DB2 database connection in Python. 24 00:01:27,120 --> 00:01:30,040 We create a connection object DSN, 25 00:01:30,040 --> 00:01:32,295 which stores the connection credentials. 26 00:01:32,295 --> 00:01:33,870 The connect function of 27 00:01:33,870 --> 00:01:39,110 the ibm_db API will be used to create a non persistent connection. 28 00:01:39,110 --> 00:01:43,230 The DSN object is passed as a parameter to the connection function. 29 00:01:43,230 --> 00:01:46,265 If a connection has been established with the database, 30 00:01:46,265 --> 00:01:48,295 then the code returns connected, 31 00:01:48,295 --> 00:01:50,085 as the output otherwise, 32 00:01:50,085 --> 00:01:53,075 the output will be unable to connect to database. 33 00:01:53,075 --> 00:01:56,335 Then we free all resources by closing the connection. 34 00:01:56,335 --> 00:01:59,300 Remember that it is always important to close 35 00:01:59,300 --> 00:02:03,275 connections so that we can avoid unused connections taking up resources. 36 00:02:03,275 --> 00:02:05,930 Thank you for watching this video. 37 00:02:05,930 --> 00:02:10,000 (Music)