├── _config.yml ├── HTML_Documentation └── readme.md ├── Python_Examples ├── readme.md ├── ibm_db_dbi │ ├── readme.md │ ├── ibm_db_dbi-connect_DB.py │ ├── ibm_db_dbi-close_CONNECTION.py │ ├── ibm_db_dbi-cursor.py │ ├── ibm_db_dbi_tools.py │ ├── ibm_db_dbi-rowcount.py │ └── ibm_db_dbi-fetchone.py └── ibm_db │ ├── readme.md │ ├── ibm_db-active.py │ ├── ibm_db-stmt_error.py │ ├── ibm_db-stmt_errormsg.py │ ├── ibm_db-close.py │ ├── ibm_db-conn_errormsg.py │ ├── ibm_db-conn_error.py │ ├── ibm_db-result.py │ ├── ipynb_exit.py │ ├── ibm_db-client_info.py │ ├── ibm_db-fetch_row.py │ ├── ibm_db-exec_immediate.py │ ├── ibm_db-field_num.py │ ├── ibm_db-createdb_LOCAL.py │ ├── ibm_db-num_fields.py │ ├── ibm_db-connect_SERVER.py │ ├── ibm_db-fetch_tuple.py │ ├── ibm_db-fetch_assoc.py │ ├── ibm_db-recreatedb_LOCAL.py │ ├── ibm_db-createdbNX_LOCAL.py │ ├── ibm_db-dropdb_LOCAL.py │ ├── ibm_db-fetch_both.py │ ├── ibm_db-createdb_REMOTE.py │ ├── ibm_db-dropdb_REMOTE.py │ ├── ibm_db-recreatedb_REMOTE.py │ └── ibm_db-createdbNX_REMOTE.py └── Jupyter_Notebooks ├── readme.md └── ipynb_exit.py /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-minimal -------------------------------------------------------------------------------- /HTML_Documentation/readme.md: -------------------------------------------------------------------------------- 1 | This directory contains HTML files that document how to use the APIs found in the ibm_db library. 2 | -------------------------------------------------------------------------------- /Python_Examples/readme.md: -------------------------------------------------------------------------------- 1 | Example programs that illustrate how to use the APIs available with the ibm_db library can be found in the ibm_db directory; examples that show how to use the APIs and functions available with the ibm_db_dbi library can be found in the ibm_db_dbi directory. 2 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/readme.md: -------------------------------------------------------------------------------- 1 | This directory contains example programs that illustrate how to use the APIs available with the ibm_db_dbi library to build applications that interact with Db2 (formerly DB2 for Linux, UNIX, and Windows) databases. 2 |
    3 |
  1. Download the following file and copy it to the directory where the Python files you download will be stored: 4 | 7 |
  2. To execute any Python file downloaded, follow the instructions provided in the file header.
  3. 8 |
9 | 10 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/readme.md: -------------------------------------------------------------------------------- 1 | This directory contains example programs that illustrate how to use the APIs available with the ibm_db library to build applications that interact with Db2 (formerly DB2 for Linux, UNIX, and Windows) databases. 2 |
    3 |
  1. Download the following files and copy them to the directory where the Python files you download will be stored: 4 | 8 |
  2. To execute any Python file downloaded, follow the instructions provided in the file header.
  3. 9 |
10 | -------------------------------------------------------------------------------- /Jupyter_Notebooks/readme.md: -------------------------------------------------------------------------------- 1 | To run these Jupyter Notebooks, perform the following steps: 2 | 3 |
    4 |
  1. Make sure the permissions for the Anaconda subdirectory are writeable to everyone! (cd to the directory where the Anaconda software was installed and execute the command chmod -R 777)
  2. 5 |
  3. Start Jupyter Notebook and open a new terminal window (New->Terminal)
  4. 6 |
  5. Execute the following commands from the terminal window: 7 | 12 |
  6. Download the following files and copy them to the directory where the Jupyter Notebooks you download will be stored: 13 | 17 |
18 | 19 | You should now be able to load any of these Jupyter Notebooks and execute the code examples presented in them. 20 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-active.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-active.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.active() API. # 6 | # # 7 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 8 | # following command from a terminal window: # 9 | # # 10 | # ./ibm_db-active.py # 11 | # # 12 | #-------------------------------------------------------------------------------------------------# 13 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 14 | # # 15 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 16 | # Licensed Materials - Property of IBM # 17 | # # 18 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 19 | # Schedule Contract with IBM Corp. # 20 | # # 21 | # The following source code ("Sample") is owned by International Business Machines Corporation # 22 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 23 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 24 | # of assisting you in the creation of Python applications using the ibm_db library. # 25 | # # 26 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 27 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 28 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 29 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 30 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 31 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 32 | # has been advised of the possibility of such damages. # 33 | #-------------------------------------------------------------------------------------------------# 34 | 35 | # Load The Appropriate Python Modules 36 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 37 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 38 | 39 | #-------------------------------------------------------------------------------------------------# 40 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 41 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 42 | # Establish And Terminate A Connection To A Db2 Server Or Database # 43 | #-------------------------------------------------------------------------------------------------# 44 | from ibm_db_tools import Db2ConnectionMgr 45 | 46 | #-------------------------------------------------------------------------------------------------# 47 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 48 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 49 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 50 | # Invoked In A Jupyter Notebook # 51 | #-------------------------------------------------------------------------------------------------# 52 | from ipynb_exit import exit 53 | 54 | # Define And Initialize The Appropriate Variables 55 | dbName = "SAMPLE" 56 | userID = "db2inst1" 57 | passWord = "Passw0rd" 58 | dbConnection = None 59 | connState = False 60 | 61 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 62 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 63 | conn.openConnection() 64 | if conn.returnCode is True: 65 | dbConnection = conn.connectionID 66 | else: 67 | conn.closeConnection() 68 | exit(-1) 69 | 70 | # Determine Whether The Current Database Connection Is Active Or Inactive 71 | try: 72 | connState = ibm_db.active(dbConnection) 73 | except Exception: 74 | pass 75 | 76 | # Display A Status Message That Shows The Current Connection State 77 | print("The connection to the " + dbName + " database is ", end="") 78 | if connState is True: 79 | print("ACTIVE.\n") 80 | elif connState is False: 81 | print("NOT ACTIVE.\n") 82 | 83 | # Close The Database Connection That Was Opened Earlier 84 | conn.closeConnection() 85 | 86 | # Return Control To The Operating System 87 | exit() 88 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi-connect_DB.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db_dbi-connect_DB.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db_dbi.connect() API to # 6 | # establish a connection to a local Db2 database. # 7 | # # 8 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 9 | # following command from a terminal window: # 10 | # # 11 | # ./ibm_db_dbi-connect_DB.py # 12 | # # 13 | #-------------------------------------------------------------------------------------------------# 14 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 15 | # # 16 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 17 | # Licensed Materials - Property of IBM # 18 | # # 19 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 20 | # Schedule Contract with IBM Corp. # 21 | # # 22 | # The following source code ("Sample") is owned by International Business Machines Corporation # 23 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 24 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 25 | # of assisting you in the creation of Python applications using the ibm_db_dbi library. # 26 | # # 27 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 28 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 29 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 30 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 31 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 32 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 33 | # has been advised of the possibility of such damages. # 34 | #-------------------------------------------------------------------------------------------------# 35 | 36 | # Load The Appropriate Python Modules 37 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 38 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 39 | 40 | # Define And Initialize The Appropriate Variables 41 | dbName = "SAMPLE" # The Alias For The Cataloged, Local Database 42 | userID = "db2inst1" # The Instance User ID At The Local Server 43 | passWord = "Passw0rd" # The Password For The Instance User ID At The Local Server 44 | connectionID = None 45 | returnCode = False 46 | 47 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 48 | # Is About To Be Made 49 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 50 | 51 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 52 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 53 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 54 | connString += ";PROTOCOL=TCPIP" 55 | connString += ";UID=" + userID 56 | connString += ";PWD=" + passWord 57 | 58 | # Attempt To Establish A Connection To The Database Specified 59 | try: 60 | connectionID = ibm_db_dbi.connect(connString, '', '') 61 | except Exception: 62 | pass 63 | 64 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 65 | if connectionID is None: 66 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 67 | print("Connection string used: " + connString + "\n") 68 | exit(-1) 69 | 70 | # Otherwise, Complete The Status Message 71 | else: 72 | print("Done!\n") 73 | 74 | 75 | # Add Additional Processing Here ... 76 | 77 | 78 | # Attempt To Close The Db2 Database Connection That Was Opened Earlier 79 | if not connectionID is None: 80 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 81 | try: 82 | returnCode = connectionID.close() 83 | except Exception: 84 | pass 85 | 86 | # If The Db2 Database Connection Was Not Closed, Display An Error Message And Exit 87 | if returnCode is False: 88 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 89 | exit(-1) 90 | 91 | # Otherwise, Complete The Status Message 92 | else: 93 | print("Done!\n") 94 | 95 | # Return Control To The Operating System 96 | exit() 97 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi-close_CONNECTION.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db_dbi-close_CONNECTION.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the .close() function of the # 6 | # IBM_DBConnection object that is returned by the ibm_db_dbi.connect() API. # 7 | # # 8 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 9 | # following command from a terminal window: # 10 | # # 11 | # ./ibm_db_dbi-close_CONNECTION.py # 12 | # # 13 | #-------------------------------------------------------------------------------------------------# 14 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 15 | # # 16 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 17 | # Licensed Materials - Property of IBM # 18 | # # 19 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 20 | # Schedule Contract with IBM Corp. # 21 | # # 22 | # The following source code ("Sample") is owned by International Business Machines Corporation # 23 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 24 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 25 | # of assisting you in the creation of Python applications using the ibm_db_dbi library. # 26 | # # 27 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 28 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 29 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 30 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 31 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 32 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 33 | # has been advised of the possibility of such damages. # 34 | #-------------------------------------------------------------------------------------------------# 35 | 36 | # Load The Appropriate Python Modules 37 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 38 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 39 | 40 | # Define And Initialize The Appropriate Variables 41 | dbName = "SAMPLE" # The Alias For The Cataloged, Local Database 42 | userID = "db2inst1" # The Instance User ID At The Local Server 43 | passWord = "Passw0rd" # The Password For The Instance User ID At The Local Server 44 | connectionID = None 45 | returnCode = False 46 | 47 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 48 | # Is About To Be Made 49 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 50 | 51 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 52 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 53 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 54 | connString += ";PROTOCOL=TCPIP" 55 | connString += ";UID=" + userID 56 | connString += ";PWD=" + passWord 57 | 58 | # Attempt To Establish A Connection To The Database Specified 59 | try: 60 | connectionID = ibm_db_dbi.connect(connString, '', '') 61 | except Exception: 62 | pass 63 | 64 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 65 | if connectionID is None: 66 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 67 | print("Connection string used: " + connString + "\n") 68 | exit(-1) 69 | 70 | # Otherwise, Complete The Status Message 71 | else: 72 | print("Done!\n") 73 | 74 | 75 | # Add Additional Processing Here ... 76 | 77 | 78 | # Attempt To Close The Db2 Database Connection That Was Opened Earlier 79 | if not connectionID is None: 80 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 81 | try: 82 | returnCode = connectionID.close() 83 | except Exception: 84 | pass 85 | 86 | # If The Db2 Database Connection Was Not Closed, Display An Error Message And Exit 87 | if returnCode is False: 88 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 89 | exit(-1) 90 | 91 | # Otherwise, Complete The Status Message 92 | else: 93 | print("Done!\n") 94 | 95 | # Return Control To The Operating System 96 | exit() 97 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi-cursor.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db_dbi-cursor.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the .cursor() function of the # 6 | # IBM_DBConnection object that is returned by the ibm_db_dbi.connect() API. # 7 | # # 8 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 9 | # following command from a terminal window: # 10 | # # 11 | # ./ibm_db_dbi-cursor.py # 12 | # # 13 | #-------------------------------------------------------------------------------------------------# 14 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 15 | # # 16 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 17 | # Licensed Materials - Property of IBM # 18 | # # 19 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 20 | # Schedule Contract with IBM Corp. # 21 | # # 22 | # The following source code ("Sample") is owned by International Business Machines Corporation # 23 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 24 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 25 | # of assisting you in the creation of Python applications using the ibm_db_dbi library. # 26 | # # 27 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 28 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 29 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 30 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 31 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 32 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 33 | # has been advised of the possibility of such damages. # 34 | #-------------------------------------------------------------------------------------------------# 35 | 36 | # Load The Appropriate Python Modules 37 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 38 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 39 | 40 | # Define And Initialize The Appropriate Variables 41 | dbName = "SAMPLE" # The Alias For The Cataloged, Local Database 42 | userID = "db2inst1" # The Instance User ID At The Local Server 43 | passWord = "Passw0rd" # The Password For The Instance User ID At The Local Server 44 | connectionID = None 45 | returnCode = False 46 | 47 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 48 | # Is About To Be Made 49 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 50 | 51 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 52 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 53 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 54 | connString += ";PROTOCOL=TCPIP" 55 | connString += ";UID=" + userID 56 | connString += ";PWD=" + passWord 57 | 58 | # Attempt To Establish A Connection To The Database Specified 59 | try: 60 | connectionID = ibm_db_dbi.connect(connString, '', '') 61 | except Exception: 62 | pass 63 | 64 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 65 | if connectionID is None: 66 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 67 | print("Connection string used: " + connString + "\n") 68 | exit(-1) 69 | 70 | # Otherwise, Complete The Status Message 71 | else: 72 | print("Done!\n") 73 | 74 | # Retrieve The Cursor Object That Was Created For The Connection Object 75 | if not connectionID is None: 76 | cursorID = connectionID.cursor() 77 | 78 | # If The Cursor Object Was Successfully Retrieved, Display Information About The Cursor Handler 79 | if not cursorID is None: 80 | print("Cursor connection handler: ", end="") 81 | print(cursorID.conn_handler) 82 | print() 83 | 84 | # Attempt To Close The Db2 Database Connection That Was Opened Earlier 85 | if not connectionID is None: 86 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 87 | try: 88 | returnCode = connectionID.close() 89 | except Exception: 90 | pass 91 | 92 | # If The Db2 Database Connection Was Not Closed, Display An Error Message And Exit 93 | if returnCode is False: 94 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 95 | exit(-1) 96 | 97 | # Otherwise, Complete The Status Message 98 | else: 99 | print("Done!\n") 100 | 101 | # Return Control To The Operating System 102 | exit() 103 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi_tools.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------------------------------# 2 | # NAME: ibm_db_tools.py # 3 | # # 4 | # PURPOSE: This file contains classes and functions that are used by some of the ibm_db_dbi # 5 | # sample programs. # 6 | # # 7 | # Cursor functions used: # 8 | # execute() # 9 | # fetchone() # 10 | # # 11 | # USAGE: Add the following line of code at the beginning of an ibm_db_dbi sample program: # 12 | # # 13 | # from ibm_db_dbi_tools import get_row_count # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of # 27 | # assisting you in the creation of Python applications using the ibm_db_dbi library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # FUNCTION NAME: get_row_count() # 44 | # PURPOSE: This function queries the Db2 table specified and displays the number of rows # 45 | # (records) found in it. # 46 | # PARAMETERS: dbCursor - A cursor object associated with a valid Db2 database connection # 47 | # tableName - Name of the table to be queried # 48 | # RETURNS: True - A row count was obtained for the table specified # 49 | # False - A row count could not be obtained for the table specified # 50 | #-------------------------------------------------------------------------------------------------# 51 | def get_row_count(dbCursor, tableName): 52 | 53 | # Define And Initialize The Appropriate Local Variables 54 | resultSet = False 55 | 56 | # Create The SQL Statement To Be Executed 57 | sqlStatement = "SELECT COUNT(*) FROM " + tableName + " WITH UR" 58 | 59 | # Execute The SQL Statement Just Defined 60 | try: 61 | resultSet = dbCursor.execute(sqlStatement) 62 | except Exception: 63 | pass 64 | 65 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 66 | if resultSet is False: 67 | print("\nERROR: Unable to execute the statement specified:") 68 | print(" " + sqlStatement + "\n") 69 | return False 70 | 71 | # Retrieve The Data Produced By The SQL Statement And Store It In A Python Tuple 72 | resultSet = None 73 | try: 74 | resultSet = dbCursor.fetchone() 75 | except: 76 | pass 77 | 78 | # If The Data Could Not Be Retrieved Or There Was No Data To Retrieve, Display An Error 79 | # Message And Return The Value "False" 80 | if resultSet is None: 81 | print("\nERROR: Unable to retrieve the data produced by the SQL statement.") 82 | return False 83 | 84 | # Otherwise, Display The Data Retrieved 85 | else: 86 | print("Number of records found in the " + tableName + " table: ", end="") 87 | print("{}\n" .format(resultSet[0])) 88 | 89 | # Return The Value "True" To The Calling Function 90 | return True 91 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-stmt_error.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-stmt_error.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.stmt_error() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-stmt_error.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | sqlStatement = " " # An Empty SQL Statement Is Used To Force An Error To Occur 63 | resultSet = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Execute The SQL Statement Specified 75 | print("Executing the SQL statement specified ... ", end="") 76 | try: 77 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 78 | except Exception: 79 | pass 80 | 81 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 82 | if resultSet is False: 83 | print("\nERROR: Unable to execute the SQL statement.") 84 | errorCode = ibm_db.stmt_error() 85 | print("\nSQLSTATE: "+ errorCode + "\n") 86 | conn.closeConnection() 87 | exit(-1) 88 | 89 | # Otherwise, Complete The Status Message 90 | else: 91 | print("Done!\n") 92 | 93 | # Close The Database Connection That Was Opened Earlier 94 | conn.closeConnection() 95 | 96 | # Return Control To The Operating System 97 | exit() 98 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-stmt_errormsg.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-stmt_errormsg.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.stmt_errormsg() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.prepare() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-stmt_errormsg.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | sqlStatement = " " # An Empty SQL Statement Is Used To Force An Error To Occur 63 | preparedStmt = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Prepare The SQL Statement Specified 75 | print("Preparing the SQL statement specified for execution ... ", end="") 76 | try: 77 | preparedStmt = ibm_db.prepare(dbConnection, sqlStatement) 78 | except Exception: 79 | pass 80 | 81 | # If Db2 Could Not Parse And Prepare The SQL Statement Correctly, Display An Error Message 82 | # And Exit 83 | if preparedStmt is False: 84 | print("\nERROR: Unable to prepare the SQL statement specified.") 85 | errorMsg = ibm_db.stmt_errormsg() 86 | print("\n" + errorMsg + "\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Close The Database Connection That Was Opened Earlier 95 | conn.closeConnection() 96 | 97 | # Return Control To The Operating System 98 | exit() 99 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-close.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-close.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.close() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.connect() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-close.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 44 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 45 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 46 | # Invoked In A Jupyter Notebook # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ipynb_exit import exit 49 | 50 | # Define And Initialize The Appropriate Variables 51 | dbName = "SAMPLE" 52 | userID = "db2inst1" 53 | passWord = "Passw0rd" 54 | dbConnection = None 55 | returnCode = False 56 | 57 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 58 | # Is About To Be Made 59 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 60 | 61 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 62 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 63 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 64 | connString += ";PROTOCOL=TCPIP" 65 | connString += ";UID=" + userID 66 | connString += ";PWD=" + passWord 67 | 68 | # Attempt To Establish A Connection To The Database Specified 69 | try: 70 | dbConnection = ibm_db.connect(connString, '', '') 71 | except Exception: 72 | pass 73 | 74 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 75 | if dbConnection is None: 76 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 77 | exit(-1) 78 | 79 | # Otherwise, Complete The Status Message 80 | else: 81 | print("Done!\n") 82 | 83 | 84 | # Add Additional Db2 Database-Related Processing Here ... 85 | 86 | 87 | # Attempt To Close The Db2 Database Connection That Was Just Opened 88 | if not dbConnection is None: 89 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 90 | try: 91 | returnCode = ibm_db.close(dbConnection) 92 | except Exception: 93 | pass 94 | 95 | # If The Database Connection Was Not Closed, Display An Error Message And Exit 96 | if returnCode is False: 97 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 98 | exit(-1) 99 | 100 | # Otherwise, Complete The Status Message 101 | else: 102 | print("Done!\n") 103 | 104 | # Return Control To The Operating System 105 | exit() 106 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-conn_errormsg.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-conn_errormsg.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.conn_errormsg() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.connect() # 9 | # ibm_db.close() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-conn_errormsg.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 45 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 46 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 47 | # Invoked In A Jupyter Notebook # 48 | #-------------------------------------------------------------------------------------------------# 49 | from ipynb_exit import exit 50 | 51 | # Define And Initialize The Appropriate Variables 52 | dbName = "SAMPLE" 53 | userID = "db2inst1" 54 | passWord = "Passw_rd" # The Wrong Password Is Used To Force An Error To Occur 55 | dbConnection = None 56 | 57 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 58 | # Is About To Be Made 59 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 60 | 61 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 62 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 63 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 64 | connString += ";PROTOCOL=TCPIP" 65 | connString += ";UID=" + userID 66 | connString += ";PWD=" + passWord 67 | 68 | # Attempt To Establish A Connection To The Database Specified 69 | try: 70 | dbConnection = ibm_db.connect(connString, '', '') 71 | except Exception: 72 | pass 73 | 74 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 75 | if dbConnection is None: 76 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 77 | errorMsg = ibm_db.conn_errormsg() 78 | print("\n" + errorMsg + "\n") 79 | exit(-1) 80 | 81 | # Otherwise, Complete The Status Message 82 | else: 83 | print("Done!\n") 84 | 85 | 86 | # Add Additional Db2 Database-Related Processing Here ... 87 | 88 | 89 | # Attempt To Close The Db2 Database Connection That Was Just Opened 90 | if not dbConnection is None: 91 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 92 | try: 93 | returnCode = ibm_db.close(dbConnection) 94 | except Exception: 95 | pass 96 | 97 | # If The Db2 Server Connection Was Not Closed, Display An Error Message And Exit 98 | if returnCode is False: 99 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 100 | errorMsg = ibm_db.conn_errormsg(dbConnection) 101 | print("\n" + errorMsg + "\n") 102 | exit(-1) 103 | 104 | # Otherwise, Complete The Status Message 105 | else: 106 | print("Done!\n") 107 | 108 | # Return Control To The Operating System 109 | exit() 110 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-conn_error.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-conn_error.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.conn_error() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.connect() # 9 | # ibm_db.close() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-conn_error.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 45 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 46 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 47 | # Invoked In A Jupyter Notebook # 48 | #-------------------------------------------------------------------------------------------------# 49 | from ipynb_exit import exit 50 | 51 | # Define And Initialize The Appropriate Variables 52 | dbName = "SAMPLE" 53 | userID = "db2inst1" 54 | passWord = "Passw_rd" # The Wrong Password Is Used To Force An Error To Occur 55 | dbConnection = None 56 | 57 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 58 | # Is About To Be Made 59 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 60 | 61 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 62 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 63 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 64 | connString += ";PROTOCOL=TCPIP" 65 | connString += ";UID=" + userID 66 | connString += ";PWD=" + passWord 67 | 68 | # Attempt To Establish A Connection To The Database Specified 69 | try: 70 | dbConnection = ibm_db.connect(connString, '', '') 71 | except Exception: 72 | pass 73 | 74 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 75 | if dbConnection is None: 76 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 77 | errorCode = ibm_db.conn_error() 78 | print("\nSQLSTATE: "+ errorCode + "\n") 79 | exit(-1) 80 | 81 | # Otherwise, Complete The Status Message 82 | else: 83 | print("Done!\n") 84 | 85 | 86 | # Add Additional Db2 Database-Related Processing Here ... 87 | 88 | 89 | # Attempt To Close The Db2 Database Connection That Was Just Opened 90 | if not dbConnection is None: 91 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 92 | try: 93 | returnCode = ibm_db.close(dbConnection) 94 | except Exception: 95 | pass 96 | 97 | # If The Db2 Server Connection Was Not Closed, Display An Error Message And Exit 98 | if returnCode is False: 99 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 100 | errorCode = ibm_db.conn_error(dbConnection) 101 | print("SQLSTATE: "+ errorCode + "\n") 102 | exit(-1) 103 | 104 | # Otherwise, Complete The Status Message 105 | else: 106 | print("Done!\n") 107 | 108 | # Return Control To The Operating System 109 | exit() 110 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-result.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-result.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.fetch_row() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # ibm_db.fetch_row() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-result.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 52 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 53 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 54 | # Invoked In A Jupyter Notebook # 55 | #-------------------------------------------------------------------------------------------------# 56 | from ipynb_exit import exit 57 | 58 | # Define And Initialize The Appropriate Variables 59 | dbName = "SAMPLE" 60 | userID = "db2inst1" 61 | passWord = "Passw0rd" 62 | dbConnection = None 63 | resultSet = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT deptname FROM department WHERE admrdept = 'A00'" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Display A Report Header 95 | print("Query results:\n") 96 | print("DEPTNAME") 97 | print("____________________________") 98 | 99 | # As Long As There Are Records In The Result Set Produced, ... 100 | while (ibm_db.fetch_row(resultSet) is True): 101 | 102 | # Display The Data Retrieved 103 | print(ibm_db.result(resultSet, 0)) 104 | 105 | # Add A Blank Line To The End Of The Report 106 | print() 107 | 108 | # Close The Database Connection That Was Opened Earlier 109 | conn.closeConnection() 110 | 111 | # Return Control To The Operating System 112 | exit() 113 | -------------------------------------------------------------------------------- /Jupyter_Notebooks/ipynb_exit.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------------------------------# 2 | # NAME: ipynb_exit.py # 3 | # # 4 | # PURPOSE: This file contains classes and functions that allows "exit()" functionality to work # 5 | # without raising an error or stopping the kernel when a Python application is # 6 | # invoked from a Jupyter Notebook. # 7 | # # 8 | # USAGE: Add the following line of code to the beginning of a Python program: # 9 | # # 10 | # from ipynb_exit import exit # 11 | # # 12 | #-------------------------------------------------------------------------------------------------# 13 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 14 | # # 15 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 16 | # Licensed Materials - Property of IBM # 17 | # # 18 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 19 | # Schedule Contract with IBM Corp. # 20 | # # 21 | # The following source code ("Sample") is owned by International Business Machines Corporation # 22 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 23 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of # 24 | # assisting you in the creation of Python applications using the ibm_db library. # 25 | # # 26 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 27 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 28 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 29 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 30 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 31 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 32 | # has been advised of the possibility of such damages. # 33 | #-------------------------------------------------------------------------------------------------# 34 | 35 | # Load The Appropriate Python Modules 36 | import sys # Provides Information About Python Interpreter Constants, 37 | # Functions, & Methods 38 | from io import StringIO # Implements A File-Like Class That Reads And Writes A String 39 | # Buffer (i.e., A Memory File) 40 | from IPython import get_ipython # Simple Function To Call To Get The Current Interactive Shell 41 | # Instance 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # CLASS NAME: ipynb_Exit() # 45 | # PURPOSE: This class contains attributes and methods that can be used to establish and # 46 | # terminate a connection to a Db2 server or database. # 47 | #-------------------------------------------------------------------------------------------------# 48 | class ipynb_Exit(SystemExit): 49 | """Exit Exception for IPython. Exception temporarily redirects stderr to buffer.""" 50 | 51 | #---------------------------------------------------------------------------------------------# 52 | # FUNCTION NAME: __init()__ # 53 | # PURPOSE: This method initializes an instance of the ipynb_Exit class. # 54 | #---------------------------------------------------------------------------------------------# 55 | def __init__(self): 56 | sys.stderr = StringIO() # Redirect sys.stderr to a StringIO (memory buffer) object. 57 | 58 | #---------------------------------------------------------------------------------------------# 59 | # FUNCTION NAME: __init()__ # 60 | # PURPOSE: This method cleans up when an instance of the ipynb_Exit class is # 61 | # deleted. # 62 | #---------------------------------------------------------------------------------------------# 63 | def __del__(self): 64 | sys.stderr = sys.__stderr__ # Restore sys.stderr to the original values it had at 65 | # the start of the program. 66 | 67 | #-------------------------------------------------------------------------------------------------# 68 | # FUNCTION: customExit() # 69 | # PURPOSE: This function contains attributes and methods that can be used to establish and # 70 | # terminate a connection to a Db2 server or database. # 71 | #-------------------------------------------------------------------------------------------------# 72 | def customExit(returnCode=0): 73 | if returnCode is 0: 74 | ipynb_Exit() 75 | else: 76 | raise ipynb_Exit 77 | 78 | #-------------------------------------------------------------------------------------------------# 79 | # If An Application Running With IPython (i.e., A Jupyter Notebook) Calls The Exit Function, # 80 | # Call A Custom Exit Routine So The Jupyter Notebook Will Not Stop Running; Otherwise, Call The # 81 | # Default Exit Routine # 82 | #-------------------------------------------------------------------------------------------------# 83 | if get_ipython(): 84 | exit = customExit # Rebind To The Custom Exit Function 85 | else: 86 | exit = exit # Just Call The Exit Function 87 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ipynb_exit.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------------------------------# 2 | # NAME: ipynb_exit.py # 3 | # # 4 | # PURPOSE: This file contains classes and functions that allows "exit()" functionality to work # 5 | # without raising an error or stopping the kernel when a Python application is # 6 | # invoked from a Jupyter Notebook. # 7 | # # 8 | # USAGE: Add the following line of code to the beginning of a Python program: # 9 | # # 10 | # from ipynb_exit import exit # 11 | # # 12 | #-------------------------------------------------------------------------------------------------# 13 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 14 | # # 15 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 16 | # Licensed Materials - Property of IBM # 17 | # # 18 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 19 | # Schedule Contract with IBM Corp. # 20 | # # 21 | # The following source code ("Sample") is owned by International Business Machines Corporation # 22 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 23 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of # 24 | # assisting you in the creation of Python applications using the ibm_db library. # 25 | # # 26 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 27 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 28 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 29 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 30 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 31 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 32 | # has been advised of the possibility of such damages. # 33 | #-------------------------------------------------------------------------------------------------# 34 | 35 | # Load The Appropriate Python Modules 36 | import sys # Provides Information About Python Interpreter Constants, 37 | # Functions, & Methods 38 | from io import StringIO # Implements A File-Like Class That Reads And Writes A String 39 | # Buffer (i.e., A Memory File) 40 | from IPython import get_ipython # Simple Function To Call To Get The Current Interactive Shell 41 | # Instance 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # CLASS NAME: ipynb_Exit() # 45 | # PURPOSE: This class contains attributes and methods that can be used to establish and # 46 | # terminate a connection to a Db2 server or database. # 47 | #-------------------------------------------------------------------------------------------------# 48 | class ipynb_Exit(SystemExit): 49 | """Exit Exception for IPython. Exception temporarily redirects stderr to buffer.""" 50 | 51 | #---------------------------------------------------------------------------------------------# 52 | # FUNCTION NAME: __init()__ # 53 | # PURPOSE: This method initializes an instance of the ipynb_Exit class. # 54 | #---------------------------------------------------------------------------------------------# 55 | def __init__(self): 56 | sys.stderr = StringIO() # Redirect sys.stderr to a StringIO (memory buffer) object. 57 | 58 | #---------------------------------------------------------------------------------------------# 59 | # FUNCTION NAME: __init()__ # 60 | # PURPOSE: This method cleans up when an instance of the ipynb_Exit class is # 61 | # deleted. # 62 | #---------------------------------------------------------------------------------------------# 63 | def __del__(self): 64 | sys.stderr = sys.__stderr__ # Restore sys.stderr to the original values it had at 65 | # the start of the program. 66 | 67 | #-------------------------------------------------------------------------------------------------# 68 | # FUNCTION: customExit() # 69 | # PURPOSE: This function contains attributes and methods that can be used to establish and # 70 | # terminate a connection to a Db2 server or database. # 71 | #-------------------------------------------------------------------------------------------------# 72 | def customExit(returnCode=0): 73 | if returnCode is 0: 74 | ipynb_Exit() 75 | else: 76 | raise ipynb_Exit 77 | 78 | #-------------------------------------------------------------------------------------------------# 79 | # If An Application Running With IPython (i.e., A Jupyter Notebook) Calls The Exit Function, # 80 | # Call A Custom Exit Routine So The Jupyter Notebook Will Not Stop Running; Otherwise, Call The # 81 | # Default Exit Routine # 82 | #-------------------------------------------------------------------------------------------------# 83 | if get_ipython(): 84 | exit = customExit # Rebind To The Custom Exit Function 85 | else: 86 | exit = exit # Just Call The Exit Function 87 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-client_info.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-client_info.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.client_info() API. # 6 | # # 7 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 8 | # following command from a terminal window: # 9 | # # 10 | # ./ibm_db-client_info.py # 11 | # # 12 | #-------------------------------------------------------------------------------------------------# 13 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 14 | # # 15 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 16 | # Licensed Materials - Property of IBM # 17 | # # 18 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 19 | # Schedule Contract with IBM Corp. # 20 | # # 21 | # The following source code ("Sample") is owned by International Business Machines Corporation # 22 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 23 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 24 | # of assisting you in the creation of Python applications using the ibm_db library. # 25 | # # 26 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 27 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 28 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 29 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 30 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 31 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 32 | # has been advised of the possibility of such damages. # 33 | #-------------------------------------------------------------------------------------------------# 34 | 35 | # Load The Appropriate Python Modules 36 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 37 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 38 | 39 | #-------------------------------------------------------------------------------------------------# 40 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 41 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 42 | # Establish And Terminate A Connection To A Db2 Server Or Database # 43 | #-------------------------------------------------------------------------------------------------# 44 | from ibm_db_tools import Db2ConnectionMgr 45 | 46 | #-------------------------------------------------------------------------------------------------# 47 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 48 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 49 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 50 | # Invoked In A Jupyter Notebook # 51 | #-------------------------------------------------------------------------------------------------# 52 | from ipynb_exit import exit 53 | 54 | # Define And Initialize The Appropriate Variables 55 | dbName = "SAMPLE" 56 | userID = "db2inst1" 57 | passWord = "Passw0rd" 58 | svrConnection = None 59 | clientInfo = False 60 | 61 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Server 62 | conn = Db2ConnectionMgr('SERVER', dbName, '', '', userID, passWord) 63 | conn.openConnection() 64 | if conn.returnCode is True: 65 | svrConnection = conn.connectionID 66 | else: 67 | conn.closeConnection() 68 | exit(-1) 69 | 70 | # Attempt To Obtain Information About The Db2 Client Being Used 71 | print("Obtaining information about the Db2 client ... ", end="") 72 | try: 73 | clientInfo = ibm_db.client_info(svrConnection) 74 | except Exception: 75 | pass 76 | 77 | # If Information About The Client Could Not Be Obtained, Display An Error Message 78 | if clientInfo is False: 79 | print("\nERROR: Unable to obtain Db2 client information.\n") 80 | 81 | # Otherwise, Complete The Status Message; Then Format And Display The Data Retrieved 82 | else: 83 | print("Done!\n") 84 | 85 | # Display A Report Header 86 | print("Client details:") 87 | print("____________________________________________________________________") 88 | 89 | # Display The Client Data 90 | print("Application code page : {}" .format(clientInfo.APPL_CODEPAGE)) 91 | print("Current connection code page : {}" .format(clientInfo.CONN_CODEPAGE)) 92 | print("Data source name (DSN) : {}" .format(clientInfo.DATA_SOURCE_NAME)) 93 | print("Driver name : {}" .format(clientInfo.DRIVER_NAME)) 94 | print("Driver version : {}" .format(clientInfo.DRIVER_VER)) 95 | print("ODBC version supported : {}" .format(clientInfo.DRIVER_ODBC_VER)) 96 | print("ODBC SQL conformance level : ", end="") 97 | if clientInfo.ODBC_SQL_CONFORMANCE == 'MINIMAL': 98 | print("Supports the minimum ODBC SQL grammar\n") 99 | elif clientInfo.ODBC_SQL_CONFORMANCE == 'CORE': 100 | print("Supports the core ODBC SQL grammar\n") 101 | elif clientInfo.ODBC_SQL_CONFORMANCE == 'EXTENDED': 102 | print("Supports extended ODBC SQL grammar\n") 103 | 104 | # Close The Server Connection That Was Opened Earlier 105 | conn.closeConnection() 106 | 107 | # Return Control To The Operating System 108 | exit() 109 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-fetch_row.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-fetch_row.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.fetch_row() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # ibm_db.result() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-fetch_row.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 52 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 53 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 54 | # Invoked In A Jupyter Notebook # 55 | #-------------------------------------------------------------------------------------------------# 56 | from ipynb_exit import exit 57 | 58 | # Define And Initialize The Appropriate Variables 59 | dbName = "SAMPLE" 60 | userID = "db2inst1" 61 | passWord = "Passw0rd" 62 | dbConnection = None 63 | resultSet = False 64 | dataValue = None 65 | 66 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 67 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 68 | conn.openConnection() 69 | if conn.returnCode is True: 70 | dbConnection = conn.connectionID 71 | else: 72 | conn.closeConnection() 73 | exit(-1) 74 | 75 | # Define The SQL Statement That Is To Be Executed 76 | sqlStatement = "SELECT deptname FROM department WHERE admrdept = 'A00'" 77 | 78 | # Execute The SQL Statement Just Defined 79 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 80 | try: 81 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 82 | except Exception: 83 | pass 84 | 85 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 86 | if resultSet is False: 87 | print("\nERROR: Unable to execute the SQL statement specified.\n") 88 | conn.closeConnection() 89 | exit(-1) 90 | 91 | # Otherwise, Complete The Status Message 92 | else: 93 | print("Done!\n") 94 | 95 | # Display A Report Header 96 | print("Query results:\n") 97 | print("DEPTNAME") 98 | print("____________________________") 99 | 100 | # As Long As There Are Records In The Result Set Produced, ... 101 | while (ibm_db.fetch_row(resultSet) is True): 102 | 103 | # Retrieve The Data From The Current Row 104 | try: 105 | dataValue = ibm_db.result(resultSet, 0) 106 | except Exception: 107 | pass 108 | 109 | # Display The Data Retrieved 110 | if not dataValue is None: 111 | print(dataValue) 112 | 113 | # Add A Blank Line To The End Of The Report 114 | print() 115 | 116 | # Close The Database Connection That Was Opened Earlier 117 | conn.closeConnection() 118 | 119 | # Return Control To The Operating System 120 | exit() 121 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-exec_immediate.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-exec_immediate.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.exec_immediate() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.fetch_row() # 9 | # ibm_db.result() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-exec_immediate.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 52 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 53 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 54 | # Invoked In A Jupyter Notebook # 55 | #-------------------------------------------------------------------------------------------------# 56 | from ipynb_exit import exit 57 | 58 | # Define And Initialize The Appropriate Variables 59 | dbName = "SAMPLE" 60 | userID = "db2inst1" 61 | passWord = "Passw0rd" 62 | dbConnection = None 63 | resultSet = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT projno, projname FROM project WHERE prstaff < 2" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Display A Report Header 95 | print("Query results:\n") 96 | print("PROJNO PROJNAME") 97 | print("______ _____________________") 98 | 99 | # As Long As There Are Records In The Result Set Produced, ... 100 | while (ibm_db.fetch_row(resultSet) is True): 101 | 102 | # Extract The Data Value For Each Column From The Current Record 103 | projectNum = ibm_db.result(resultSet, 0) 104 | projectName = ibm_db.result(resultSet, 1) 105 | 106 | # Format And Display The Data Retrieved 107 | print("{:6} {:24}" .format(projectNum, projectName)) 108 | 109 | # Add A Blank Line To The End Of The Report 110 | print() 111 | 112 | # Close The Database Connection That Was Opened Earlier 113 | conn.closeConnection() 114 | 115 | # Return Control To The Operating System 116 | exit() 117 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-field_num.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-field_num.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.field_num() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-field_num.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | resultSet = False 63 | columnNumber = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT * FROM employee" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Find Out Which Column In The Result Set Produced By The Query Just Executed Contains Data 95 | # That Was Retrieved From The "BIRTHDATE" Column Of The Table 96 | try: 97 | columnNumber = ibm_db.field_num(resultSet, 'BIRTHDATE') 98 | except Exception: 99 | pass 100 | 101 | # If The Column Number For The "BIRTHDATE" Column Could Not Be Obtained, Display An Error 102 | # Message And Exit 103 | if columnNumber is False: 104 | print("\nERROR: Unable to obtain information about the result set produced.") 105 | conn.closeConnection() 106 | exit(-1) 107 | 108 | # Format And Display The Data Retrieved 109 | print("\'BIRTHDATE\' data can be found in column number {:<2} " .format(columnNumber), end="") 110 | print("of the result set produced.\n") 111 | 112 | # Close The Database Connection That Was Opened Earlier 113 | conn.closeConnection() 114 | 115 | # Return Control To The Operating System 116 | exit() 117 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-createdb_LOCAL.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-createdb_LOCAL.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.createdb() API to # 6 | # create a database on a local Db2 server. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-createdb_LOCAL.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | userID = "db2inst1" # User ID (Recognized By The Local Server) 67 | passWord = "Passw0rd" # User Password 68 | svrConnection = None 69 | dbName = "MY_DB" 70 | returnCode = False 71 | 72 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To The Local Db2 Server 73 | conn = Db2ConnectionMgr('LOCAL_SVR', '', '', '', userID, passWord) 74 | conn.openConnection() 75 | if conn.returnCode is True: 76 | svrConnection = conn.connectionID 77 | else: 78 | conn.closeConnection() 79 | exit(-1) 80 | 81 | # Attempt To Create A New Database At The Local Server 82 | print("Creating a database named " + dbName + " at the local server. Please wait.") 83 | try: 84 | returnCode = ibm_db.createdb(svrConnection, dbName) 85 | except Exception: 86 | pass 87 | 88 | # If The Database Could Not Be Created, Display An Error Message And Exit 89 | if returnCode is False: 90 | print("ERROR: Unable to create the " + dbName + " database.\n") 91 | errorMsg = ibm_db.conn_errormsg(svrConnection) 92 | print(errorMsg + "\n") 93 | conn.closeConnection() 94 | exit(-1) 95 | 96 | # Otherwise, Display A Status Message 97 | else: 98 | print("\nThe database \"" + dbName + "\" has been created!\n") 99 | query_sdb_dir(dbName) 100 | 101 | # Close The Db2 Server Connection That Was Opened Earlier 102 | conn.closeConnection() 103 | 104 | # Return Control To The Operating System 105 | exit() 106 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-num_fields.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-num_fields.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db-num_fields() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-num_fields.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | resultSet = False 63 | numColumns = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT * FROM project WHERE prstaff < 5" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Find Out How Many Columns Were Returned In The Result Set Produced By The Query Just Executed 95 | print("Examining the result set produced ... ", end="") 96 | try: 97 | numColumns = ibm_db.num_fields(resultSet) 98 | except Exception: 99 | pass 100 | 101 | # If Information About The Number Columns Returned Could Not Be Obtained, Display An Error 102 | # Message And Exit 103 | if numColumns is False: 104 | print("\nERROR: Unable to obtain information about the result set produced.") 105 | conn.closeConnection() 106 | exit(-1) 107 | 108 | # Otherwise, Complete The Status Message 109 | else: 110 | print("Done!\n") 111 | 112 | # Display The Information Retrieved 113 | print("There are " + str(numColumns) + " columns in the result set produced by the query.\n") 114 | 115 | # Close The Database Connection That Was Opened Earlier 116 | conn.closeConnection() 117 | 118 | # Return Control To The Operating System 119 | exit() 120 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-connect_SERVER.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-connect_SERVER.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.connect() API to # 6 | # establish a connection to a Db2 server. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.close() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-connect_SERVER.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 45 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 46 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 47 | # Invoked In A Jupyter Notebook # 48 | #-------------------------------------------------------------------------------------------------# 49 | from ipynb_exit import exit 50 | 51 | # Define And Initialize The Appropriate Variables 52 | hostName = "197.126.80.22" # IP Address Of Remote Server 53 | portNum = "50000" # Port Number That Receives Db2 Connections On The Remote Server 54 | userID = "db2inst2" # The Instance User ID At The Remote Server 55 | passWord = "ibmdb2" # The Password For The Instance User ID At The Remote Server 56 | connectionID = None 57 | 58 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Server 59 | # Is About To Be Made 60 | print("\nConnecting to the \'" + hostName + "\' server ... ", end="") 61 | 62 | # Construct The String That Will Be Used To Establish A Db2 Server Connection 63 | connString = "DRIVER={IBM DB2 ODBC DRIVER}" 64 | connString += ";ATTACH=TRUE" # Attach To A Server; Not A Database 65 | connString += ";DATABASE=" # Ignored When Connecting To A Server 66 | connString += ";HOSTNAME=" + hostName # Required To Connect To A Server 67 | connString += ";PORT=" + portNum # Required To Connect To A Server 68 | connString += ";PROTOCOL=TCPIP" # Required To Connect To A Server 69 | connString += ";UID=" + userID 70 | connString += ";PWD=" + passWord 71 | 72 | # Attempt To Establish A Connection To The Server Specified 73 | try: 74 | connectionID = ibm_db.connect(connString, '', '') 75 | except Exception: 76 | pass 77 | 78 | # If A Db2 Server Connection Could Not Be Established, Display An Error Message And Exit 79 | if connectionID is None: 80 | print("\nERROR: Unable to connect to the \'" + hostName + "\' server.") 81 | print("Connection string used: " + connString + "\n") 82 | exit(-1) 83 | 84 | # Otherwise, Complete The Status Message 85 | else: 86 | print("Done!\n") 87 | 88 | 89 | # Add Additional Db2 Server-Related Processing Here ... 90 | # For Example, ibm_db.createdb(), ibm_db.createdbNX(), ibm_db.recreatedb(), ibm_db.dropdb() 91 | 92 | 93 | # Attempt To Close The Db2 Server Connection That Was Just Opened 94 | if not connectionID is None: 95 | print("Disconnecting from the \'" + hostName + "\' server ... ", end="") 96 | try: 97 | returnCode = ibm_db.close(connectionID) 98 | except Exception: 99 | pass 100 | 101 | # If The Db2 Server Connection Was Not Closed, Display An Error Message And Exit 102 | if returnCode is False: 103 | print("\nERROR: Unable to disconnect from the " + hostName + " server.") 104 | exit(-1) 105 | 106 | # Otherwise, Complete The Status Message 107 | else: 108 | print("Done!\n") 109 | 110 | # Return Control To The Operating System 111 | exit() 112 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-fetch_tuple.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-fetch_tuple.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.fetch_tuple() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-fetch_tuple.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | resultSet = False 63 | dataRecord = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT deptname FROM department WHERE admrdept = 'A00'" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Display A Report Header 95 | print("Query results:\n") 96 | print("DEPTNAME") 97 | print("____________________________") 98 | 99 | # As Long As There Are Records In The Result Set Produced, ... 100 | noData = False 101 | while noData is False: 102 | 103 | # Retrieve A Record And Store It In A Python Tuple 104 | try: 105 | dataRecord = ibm_db.fetch_tuple(resultSet) 106 | except: 107 | pass 108 | 109 | # If The Data Could Not Be Retrieved Or There Was No Data To Retrieve, Set The 110 | # "No Data" Flag And Exit The Loop 111 | if dataRecord is False: 112 | noData = True 113 | 114 | # Otherwise, Display The Data Retrieved 115 | else: 116 | print(dataRecord[0]) 117 | 118 | # Add A Blank Line To The End Of The Report 119 | print() 120 | 121 | # Close The Database Connection That Was Opened Earlier 122 | conn.closeConnection() 123 | 124 | # Return Control To The Operating System 125 | exit() 126 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-fetch_assoc.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-fetch_assoc.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.fetch_assoc() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-fetch_assoc.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | resultSet = False 63 | dataRecord = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT deptname FROM department WHERE admrdept = 'A00'" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Display A Report Header 95 | print("Query results:\n") 96 | print("DEPTNAME") 97 | print("____________________________") 98 | 99 | # As Long As There Are Records In The Result Set Produced, ... 100 | noData = False 101 | while noData is False: 102 | 103 | # Retrieve A Record And Store It In A Python Dictionary 104 | try: 105 | dataRecord = ibm_db.fetch_assoc(resultSet) 106 | except: 107 | pass 108 | 109 | # If The Data Could Not Be Retrieved Or There Was No Data To Retrieve, Set The 110 | # "No Data" Flag And Exit The Loop 111 | if dataRecord is False: 112 | noData = True 113 | 114 | # Otherwise, Display The Data Retrieved 115 | else: 116 | print("{:20}" .format(dataRecord['DEPTNAME'])) 117 | 118 | # Add A Blank Line To The End Of The Report 119 | print() 120 | 121 | # Close The Database Connection That Was Opened Earlier 122 | conn.closeConnection() 123 | 124 | # Return Control To The Operating System 125 | exit() 126 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi-rowcount.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db_dbi-rowcount.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to display the .rowcount attribute # 6 | # information for the cursor object associated with the IBM_DBConnection object # 7 | # returned by the ibm_db_dbi.connect() API. # 8 | # # 9 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 10 | # following command from a terminal window: # 11 | # # 12 | # ./ibm_db_dbi-rowcount.py # 13 | # # 14 | #-------------------------------------------------------------------------------------------------# 15 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 16 | # # 17 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 18 | # Licensed Materials - Property of IBM # 19 | # # 20 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 21 | # Schedule Contract with IBM Corp. # 22 | # # 23 | # The following source code ("Sample") is owned by International Business Machines Corporation # 24 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 25 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 26 | # of assisting you in the creation of Python applications using the ibm_db_dbi library. # 27 | # # 28 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 29 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 30 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 31 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 32 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 33 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 34 | # has been advised of the possibility of such damages. # 35 | #-------------------------------------------------------------------------------------------------# 36 | 37 | # Load The Appropriate Python Modules 38 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 39 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 40 | 41 | # Define And Initialize The Appropriate Variables 42 | dbName = "SAMPLE" # The Alias For The Cataloged, Local Database 43 | userID = "db2inst1" # The Instance User ID At The Local Server 44 | passWord = "Passw0rd" # The Password For The Instance User ID At The Local Server 45 | connectionID = None 46 | resultSet = False 47 | returnCode = False 48 | 49 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 50 | # Is About To Be Made 51 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 52 | 53 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 54 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 55 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 56 | connString += ";PROTOCOL=TCPIP" 57 | connString += ";UID=" + userID 58 | connString += ";PWD=" + passWord 59 | 60 | # Attempt To Establish A Connection To The Database Specified 61 | try: 62 | connectionID = ibm_db_dbi.connect(connString, '', '') 63 | except Exception: 64 | pass 65 | 66 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 67 | if connectionID is None: 68 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 69 | print("Connection string used: " + connString + "\n") 70 | exit(-1) 71 | 72 | # Otherwise, Complete The Status Message 73 | else: 74 | print("Done!\n") 75 | 76 | # Retrieve The Cursor Object That Was Created For The Connection Object 77 | if not connectionID is None: 78 | cursorID = connectionID.cursor() 79 | 80 | # Define The INSERT Statement (With Parameter Markers) That Is To Be Used To Add Data 81 | # To The DEPARTMENT Table 82 | sqlStatement = "INSERT INTO department VALUES(?, ?, ?, 'E01', NULL)" 83 | 84 | # Create A List Of Data Values That Are To Be Supplied For The Parameter Markers Coded 85 | # In The INSERT Statement Specified 86 | pmValues = (('K22', 'SALES', '000110'), 87 | ('L22', 'FINANCE', '000120'), 88 | ('M22', 'HUMAN RESOURCES', '000130')) 89 | 90 | # Execute The SQL Statement Just Defined 91 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 92 | try: 93 | resultSet = cursorID.executemany(sqlStatement, pmValues) 94 | except Exception: 95 | pass 96 | 97 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 98 | if resultSet is False: 99 | print("\nERROR: Unable to execute the SQL statement specified.\n") 100 | connectionID.close() 101 | exit(-1) 102 | 103 | # Otherwise, Complete The Status Message And Display The Number Of Records Added 104 | else: 105 | rowsAffected = cursorID.rowcount 106 | print("Done!\n") 107 | print("Number of records added : {}\n" .format(rowsAffected)) 108 | 109 | # Attempt To Close The Db2 Database Connection That Was Opened Earlier 110 | if not connectionID is None: 111 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 112 | try: 113 | returnCode = connectionID.close() 114 | except Exception: 115 | pass 116 | 117 | # If The Db2 Database Connection Was Not Closed, Display An Error Message And Exit 118 | if returnCode is False: 119 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 120 | exit(-1) 121 | 122 | # Otherwise, Complete The Status Message 123 | else: 124 | print("Done!\n") 125 | 126 | # Return Control To The Operating System 127 | exit() 128 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-recreatedb_LOCAL.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-recreatedb_LOCAL.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.recreatedb() API to # 6 | # drop and recreate a local Db2 database. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-recreatedb_LOCAL.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | userID = "db2inst1" # User ID (Recognized By The Local Server) 67 | passWord = "Passw0rd" # User Password 68 | svrConnection = None 69 | dbName = "MY_DB" 70 | returnCode = False 71 | 72 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To The Local Db2 Server 73 | conn = Db2ConnectionMgr('LOCAL_SVR', '', '', '', userID, passWord) 74 | conn.openConnection() 75 | if conn.returnCode is True: 76 | svrConnection = conn.connectionID 77 | else: 78 | conn.closeConnection() 79 | exit(-1) 80 | 81 | # Attempt To Drop And Recreate A Database At The Local Server 82 | print("Dropping and recreating a database named " + dbName + ". Please wait.") 83 | try: 84 | returnCode = ibm_db.recreatedb(svrConnection, dbName, 'UTF-8') 85 | except Exception: 86 | pass 87 | 88 | # If The Database Could Not Be Recreated, Display An Error Message And Exit 89 | if returnCode is False: 90 | print("ERROR: Unable to drop and recreate the " + dbName + " database.\n") 91 | errorMsg = ibm_db.conn_errormsg(svrConnection) 92 | print(errorMsg + "\n") 93 | 94 | # Otherwise, Display A Status Message And Verify That Information About The Database 95 | # That Was Just Recreated Exists In The Db2 System Database Directory 96 | else: 97 | print("\nThe database \"" + dbName + "\" has been recreated!\n") 98 | query_sdb_dir(dbName) 99 | 100 | # Close The Db2 Server Connection That Was Opened Earlier 101 | conn.closeConnection() 102 | 103 | # Return Control To The Operating System 104 | exit() 105 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-createdbNX_LOCAL.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-createdbNX_LOCAL.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.createdbNX() API to # 6 | # create a local Db2 database without generating an error if that database already # 7 | # exists. # 8 | # # 9 | # Additional APIs used: # 10 | # ibm_db.conn_errormsg() # 11 | # # 12 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 13 | # following command from a terminal window: # 14 | # # 15 | # ./ibm_db-createdbNX_LOCAL.py # 16 | # # 17 | #-------------------------------------------------------------------------------------------------# 18 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 19 | # # 20 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 21 | # Licensed Materials - Property of IBM # 22 | # # 23 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 24 | # Schedule Contract with IBM Corp. # 25 | # # 26 | # The following source code ("Sample") is owned by International Business Machines Corporation # 27 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 28 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 29 | # of assisting you in the creation of Python applications using the ibm_db library. # 30 | # # 31 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 32 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 33 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 34 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 35 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 36 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 37 | # has been advised of the possibility of such damages. # 38 | #-------------------------------------------------------------------------------------------------# 39 | 40 | # Load The Appropriate Python Modules 41 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 42 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 43 | 44 | #-------------------------------------------------------------------------------------------------# 45 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 46 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 47 | # Establish And Terminate A Connection To A Db2 Server Or Database # 48 | #-------------------------------------------------------------------------------------------------# 49 | from ibm_db_tools import Db2ConnectionMgr 50 | 51 | #-------------------------------------------------------------------------------------------------# 52 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 53 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 54 | # Database Can Be Found In The Db2 System Database Directory. # 55 | #-------------------------------------------------------------------------------------------------# 56 | from ibm_db_tools import query_sdb_dir 57 | 58 | #-------------------------------------------------------------------------------------------------# 59 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 60 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 61 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 62 | # Invoked In A Jupyter Notebook # 63 | #-------------------------------------------------------------------------------------------------# 64 | from ipynb_exit import exit 65 | 66 | # Define And Initialize The Appropriate Variables 67 | userID = "db2inst1" # User ID (Recognized By The Local Server) 68 | passWord = "Passw0rd" # User Password 69 | svrConnection = None 70 | dbName = "MY_DB" 71 | returnCode = False 72 | 73 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To The Local Db2 Server 74 | conn = Db2ConnectionMgr('LOCAL_SVR', '', '', '', userID, passWord) 75 | conn.openConnection() 76 | if conn.returnCode is True: 77 | svrConnection = conn.connectionID 78 | else: 79 | conn.closeConnection() 80 | exit(-1) 81 | 82 | # Attempt To Create A New Database At The Local Server 83 | print("Creating a database named " + dbName + " at the local server. Please wait.") 84 | try: 85 | returnCode = ibm_db.createdbNX(svrConnection, dbName) 86 | except Exception: 87 | pass 88 | 89 | # If The Database Could Not Be Created, Display An Error Message And Exit 90 | if returnCode is False: 91 | print("ERROR: Unable to create the " + dbName + " database.\n") 92 | errorMsg = ibm_db.conn_errormsg(svrConnection) 93 | print(errorMsg + "\n") 94 | conn.closeConnection() 95 | exit(-1) 96 | 97 | # Otherwise, Display A Status Message 98 | else: 99 | print("\nThe database \"" + dbName + "\" has been created!\n") 100 | query_sdb_dir(dbName) 101 | 102 | # Close The Db2 Server Connection That Was Opened Earlier 103 | conn.closeConnection() 104 | 105 | # Return Control To The Operating System 106 | exit() 107 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-dropdb_LOCAL.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-dropdb_LOCAL.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.dropdb() API to delete # 6 | # (drop) a local Db2 database. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-dropdb_REMOTE.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 23 | # Licensed Materials - Property of IBM # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | userID = "db2inst1" # User ID (Recognized By The Local Server) 67 | passWord = "Passw0rd" # User Password 68 | svrConnection = None 69 | dbName = "MY_DB" 70 | returnCode = False 71 | 72 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Remote Db2 Server 73 | conn = Db2ConnectionMgr('LOCAL_SVR', '', '', '', userID, passWord) 74 | conn.openConnection() 75 | if conn.returnCode is True: 76 | svrConnection = conn.connectionID 77 | else: 78 | conn.closeConnection() 79 | exit(-1) 80 | 81 | # Attempt To Delete (Drop) A Database At The Remote Server 82 | print("Dropping a database named " + dbName + " at the local server. Please wait.") 83 | try: 84 | returnCode = ibm_db.dropdb(svrConnection, dbName) 85 | except Exception: 86 | pass 87 | 88 | # If The Database Could Not Be Deleted, Display An Error Message And Exit 89 | if returnCode is False: 90 | print("ERROR: Unable to drop the " + dbName + " database.\n") 91 | errorMsg = ibm_db.conn_errormsg(svrConnection) 92 | print(errorMsg + "\n") 93 | conn.closeConnection() 94 | exit(-1) 95 | 96 | # Otherwise, Display A Status Message And Verify That Information About The Database 97 | # That Was Just Deleted No Longer Exists In The Db2 System Database Directory 98 | else: 99 | print("\nThe database \"" + dbName + "\" has been deleted!\n") 100 | query_sdb_dir(dbName) 101 | 102 | # Close The Db2 Server Connection That Was Opened Earlier 103 | conn.closeConnection() 104 | 105 | # Return Control To The Operating System 106 | exit() 107 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-fetch_both.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-fetch_both.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.fetch_both() API. # 6 | # # 7 | # Additional APIs used: # 8 | # ibm_db.exec_immediate() # 9 | # # 10 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 11 | # following command from a terminal window: # 12 | # # 13 | # ./ibm_db-fetch_both.py # 14 | # # 15 | #-------------------------------------------------------------------------------------------------# 16 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 17 | # # 18 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 19 | # Licensed Materials - Property of IBM # 20 | # # 21 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 22 | # Schedule Contract with IBM Corp. # 23 | # # 24 | # The following source code ("Sample") is owned by International Business Machines Corporation # 25 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 26 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 27 | # of assisting you in the creation of Python applications using the ibm_db library. # 28 | # # 29 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 30 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 31 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 32 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 33 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 34 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 35 | # has been advised of the possibility of such damages. # 36 | #-------------------------------------------------------------------------------------------------# 37 | 38 | # Load The Appropriate Python Modules 39 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 40 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 41 | 42 | #-------------------------------------------------------------------------------------------------# 43 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 44 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 45 | # Establish And Terminate A Connection To A Db2 Server Or Database # 46 | #-------------------------------------------------------------------------------------------------# 47 | from ibm_db_tools import Db2ConnectionMgr 48 | 49 | #-------------------------------------------------------------------------------------------------# 50 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 51 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 52 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 53 | # Invoked In A Jupyter Notebook # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ipynb_exit import exit 56 | 57 | # Define And Initialize The Appropriate Variables 58 | dbName = "SAMPLE" 59 | userID = "db2inst1" 60 | passWord = "Passw0rd" 61 | dbConnection = None 62 | resultSet = False 63 | dataRecord = False 64 | 65 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Database 66 | conn = Db2ConnectionMgr('DB', dbName, '', '', userID, passWord) 67 | conn.openConnection() 68 | if conn.returnCode is True: 69 | dbConnection = conn.connectionID 70 | else: 71 | conn.closeConnection() 72 | exit(-1) 73 | 74 | # Define The SQL Statement That Is To Be Executed 75 | sqlStatement = "SELECT deptno, deptname FROM department WHERE admrdept = 'A00'" 76 | 77 | # Execute The SQL Statement Just Defined 78 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 79 | try: 80 | resultSet = ibm_db.exec_immediate(dbConnection, sqlStatement) 81 | except Exception: 82 | pass 83 | 84 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 85 | if resultSet is False: 86 | print("\nERROR: Unable to execute the SQL statement specified.\n") 87 | conn.closeConnection() 88 | exit(-1) 89 | 90 | # Otherwise, Complete The Status Message 91 | else: 92 | print("Done!\n") 93 | 94 | # Display A Report Header 95 | print("Query results:\n") 96 | print("DEPTNO DEPTNAME") 97 | print("______ ______________________") 98 | 99 | # As Long As There Are Records In The Result Set Produced, ... 100 | noData = False 101 | while noData is False: 102 | 103 | # Retrieve A Record And Store It In A Python Dictionary And A Python Tuple 104 | try: 105 | dataRecord = ibm_db.fetch_both(resultSet) 106 | except: 107 | pass 108 | 109 | # If The Data Could Not Be Retrieved Or There Was No Data To Retrieve, Set The 110 | # "No Data" Flag And Exit The Loop 111 | if dataRecord is False: 112 | noData = True 113 | 114 | # Otherwise, Display The Data Retrieved 115 | else: 116 | print("{:6} ".format(dataRecord[0]), end="") # Tuple Value 117 | print("{:20}" .format(dataRecord['DEPTNAME'])) # Dictionary Value 118 | 119 | # Add A Blank Line To The End Of The Report 120 | print() 121 | 122 | # Close The Database Connection That Was Opened Earlier 123 | conn.closeConnection() 124 | 125 | # Return Control To The Operating System 126 | exit() 127 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-createdb_REMOTE.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-createdb_REMOTE.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.createdb() API to # 6 | # create a database on a remote Db2 server. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-createdb_REMOTE.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | hostName = "197.126.80.22" # IP Address Of A Remote Server 67 | portNum = "50000" # Port Number Used By Db2 68 | userID = "db2inst2" # User ID (Recognized By The Remote Server) 69 | passWord = "ibmdb2" # User Password 70 | svrConnection = None 71 | dbName = "MY_DB" 72 | returnCode = False 73 | 74 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Server 75 | conn = Db2ConnectionMgr('SERVER', '', hostName, portNum, userID, passWord) 76 | conn.openConnection() 77 | if conn.returnCode is True: 78 | svrConnection = conn.connectionID 79 | else: 80 | conn.closeConnection() 81 | exit(-1) 82 | 83 | # Attempt To Create A New Database At The Remote Server 84 | print("Creating a database named " + dbName + " at the " + hostName + " server. Please wait.") 85 | try: 86 | returnCode = ibm_db.createdb(svrConnection, dbName) 87 | except Exception: 88 | pass 89 | 90 | # If The Database Could Not Be Created, Display An Error Message And Exit 91 | if returnCode is False: 92 | print("ERROR: Unable to create the " + dbName + " database.\n") 93 | errorMsg = ibm_db.conn_errormsg(svrConnection) 94 | print(errorMsg + "\n") 95 | conn.closeConnection() 96 | exit(-1) 97 | 98 | # Otherwise, Display A Status Message And Verify That Information About The Database 99 | # That Was Just Created Exists In The Db2 System Database Directory 100 | else: 101 | print("\nThe database \"" + dbName + "\" has been created!\n") 102 | query_sdb_dir(dbName) 103 | 104 | # Close The Db2 Server Connection That Was Opened Earlier 105 | conn.closeConnection() 106 | 107 | # Return Control To The Operating System 108 | exit() 109 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-dropdb_REMOTE.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-dropdb_REMOTE.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.dropdb() API to delete # 6 | # (drop) a database stored on a remote Db2 server. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-dropdb_REMOTE.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | hostName = "197.126.80.22" # IP Address Of A Remote Server 67 | portNum = "50000" # Port Number Used By Db2 68 | userID = "db2inst2" # User ID (Recognized By The Remote Server) 69 | passWord = "ibmdb2" # User Password 70 | svrConnection = None 71 | dbName = "MY_DB" 72 | returnCode = False 73 | 74 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Remote Db2 Server 75 | conn = Db2ConnectionMgr('SERVER', '', hostName, portNum, userID, passWord) 76 | conn.openConnection() 77 | if conn.returnCode is True: 78 | svrConnection = conn.connectionID 79 | else: 80 | conn.closeConnection() 81 | exit(-1) 82 | 83 | # Attempt To Delete (Drop) A Database At The Remote Server 84 | print("Dropping a database named " + dbName + " at the " + hostName + " server. Please wait.") 85 | try: 86 | returnCode = ibm_db.dropdb(svrConnection, dbName) 87 | except Exception: 88 | pass 89 | 90 | # If The Database Could Not Be Deleted, Display An Error Message And Exit 91 | if returnCode is False: 92 | print("ERROR: Unable to drop the " + dbName + " database.\n") 93 | errorMsg = ibm_db.conn_errormsg(svrConnection) 94 | print(errorMsg + "\n") 95 | conn.closeConnection() 96 | exit(-1) 97 | 98 | # Otherwise, Display A Status Message And Verify That Information About The Database 99 | # That Was Just Deleted No Longer Exists In The Db2 System Database Directory 100 | else: 101 | print("\nThe database \"" + dbName + "\" has been deleted!\n") 102 | query_sdb_dir(dbName) 103 | 104 | # Close The Db2 Server Connection That Was Opened Earlier 105 | conn.closeConnection() 106 | 107 | # Return Control To The Operating System 108 | exit() 109 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-recreatedb_REMOTE.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-recreatedb_REMOTE.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.recreatedb() API to # 6 | # drop and recreate a database on a remote Db2 server. # 7 | # # 8 | # Additional APIs used: # 9 | # ibm_db.conn_errormsg() # 10 | # # 11 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 12 | # following command from a terminal window: # 13 | # # 14 | # ./ibm_db-recreatedb_REMOTE.py # 15 | # # 16 | #-------------------------------------------------------------------------------------------------# 17 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 18 | # # 19 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 20 | # Licensed Materials - Property of IBM # 21 | # # 22 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 23 | # Schedule Contract with IBM Corp. # 24 | # # 25 | # The following source code ("Sample") is owned by International Business Machines Corporation # 26 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 27 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 28 | # of assisting you in the creation of Python applications using the ibm_db library. # 29 | # # 30 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 31 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 32 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 33 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 34 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 35 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 36 | # has been advised of the possibility of such damages. # 37 | #-------------------------------------------------------------------------------------------------# 38 | 39 | # Load The Appropriate Python Modules 40 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 41 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 42 | 43 | #-------------------------------------------------------------------------------------------------# 44 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 45 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 46 | # Establish And Terminate A Connection To A Db2 Server Or Database # 47 | #-------------------------------------------------------------------------------------------------# 48 | from ibm_db_tools import Db2ConnectionMgr 49 | 50 | #-------------------------------------------------------------------------------------------------# 51 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 52 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 53 | # Database Can Be Found In The Db2 System Database Directory. # 54 | #-------------------------------------------------------------------------------------------------# 55 | from ibm_db_tools import query_sdb_dir 56 | 57 | #-------------------------------------------------------------------------------------------------# 58 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 59 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 60 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 61 | # Invoked In A Jupyter Notebook # 62 | #-------------------------------------------------------------------------------------------------# 63 | from ipynb_exit import exit 64 | 65 | # Define And Initialize The Appropriate Variables 66 | hostName = "197.126.80.22" # IP Address Of A Remote Server 67 | portNum = "50000" # Port Number Used By Db2 68 | userID = "db2inst2" # User ID (Recognized By The Remote Server) 69 | passWord = "ibmdb2" # User Password 70 | svrConnection = None 71 | dbName = "MY_DB" 72 | returnCode = False 73 | 74 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Remote Db2 Server 75 | conn = Db2ConnectionMgr('SERVER', '', hostName, portNum, userID, passWord) 76 | conn.openConnection() 77 | if conn.returnCode is True: 78 | svrConnection = conn.connectionID 79 | else: 80 | conn.closeConnection() 81 | exit(-1) 82 | 83 | # Attempt To Delete (Drop) And Recreate A Database At The Remote Server 84 | print("Dropping and recreating a database named " + dbName + " at the " + hostName + " server. Please wait.") 85 | try: 86 | returnCode = ibm_db.recreatedb(svrConnection, dbName, 'UTF-8') 87 | except Exception: 88 | pass 89 | 90 | # If The Database Could Not Be Recreated, Display An Error Message And Exit 91 | if returnCode is False: 92 | print("\nERROR: Unable to drop and recreate the " + dbName + " database.\n") 93 | errorMsg = ibm_db.conn_errormsg(svrConnection) 94 | print(errorMsg + "\n") 95 | conn.closeConnection() 96 | exit(-1) 97 | 98 | # Otherwise, Display A Status Message And Verify That Information About The Database 99 | # That Was Just Recreated Exists In The Db2 System Database Directory 100 | else: 101 | print("\nThe database \"" + dbName + "\" has been recreated!\n") 102 | query_sdb_dir(dbName) 103 | 104 | # Close The Db2 Server Connection That Was Opened Earlier 105 | conn.closeConnection() 106 | 107 | # Return Control To The Operating System 108 | exit() 109 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db/ibm_db-createdbNX_REMOTE.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db-createdbNX_REMOTE.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the ibm_db.createdbNX() API to # 6 | # create a database on a remote Db2 server without generating an error if that # 7 | # database already exists. # 8 | # # 9 | # Additional APIs used: # 10 | # ibm_db.conn_errormsg() # 11 | # # 12 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 13 | # following command from a terminal window: # 14 | # # 15 | # ./ibm_db-createdbNX_REMOTE.py # 16 | # # 17 | #-------------------------------------------------------------------------------------------------# 18 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 19 | # # 20 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 21 | # Licensed Materials - Property of IBM # 22 | # # 23 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 24 | # Schedule Contract with IBM Corp. # 25 | # # 26 | # The following source code ("Sample") is owned by International Business Machines Corporation # 27 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 28 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 29 | # of assisting you in the creation of Python applications using the ibm_db library. # 30 | # # 31 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 32 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 33 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 34 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 35 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 36 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 37 | # has been advised of the possibility of such damages. # 38 | #-------------------------------------------------------------------------------------------------# 39 | 40 | # Load The Appropriate Python Modules 41 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 42 | import ibm_db # Contains The APIs Needed To Work With Db2 Databases 43 | 44 | #-------------------------------------------------------------------------------------------------# 45 | # Import The Db2ConnectionMgr Class Definition, Attributes, And Methods That Have Been Defined # 46 | # In The File Named "ibm_db_tools.py"; This Class Contains The Programming Logic Needed To # 47 | # Establish And Terminate A Connection To A Db2 Server Or Database # 48 | #-------------------------------------------------------------------------------------------------# 49 | from ibm_db_tools import Db2ConnectionMgr 50 | 51 | #-------------------------------------------------------------------------------------------------# 52 | # Import The query_sdb_dir() Function That Has Been Defined In The File Named "ibm_db_tools.py"; # 53 | # This Function Contains The Programming Logic Needed To See If Information About A Specific # 54 | # Database Can Be Found In The Db2 System Database Directory. # 55 | #-------------------------------------------------------------------------------------------------# 56 | from ibm_db_tools import query_sdb_dir 57 | 58 | #-------------------------------------------------------------------------------------------------# 59 | # Import The ipynb_exit Class Definition, Attributes, And Methods That Have Been Defined In The # 60 | # File Named "ipynb_exit.py"; This Class Contains The Programming Logic Needed To Allow "exit()" # 61 | # Functionality To Work Without Raising An Error Or Stopping The Kernel If The Application Is # 62 | # Invoked In A Jupyter Notebook # 63 | #-------------------------------------------------------------------------------------------------# 64 | from ipynb_exit import exit 65 | 66 | # Define And Initialize The Appropriate Variables 67 | hostName = "197.126.80.22" # IP Address Of A Remote Server 68 | portNum = "50000" # Port Number Used By Db2 69 | userID = "db2inst2" # User ID (Recognized By The Remote Server) 70 | passWord = "ibmdb2" # User Password 71 | svrConnection = None 72 | dbName = "MY_DB" 73 | returnCode = False 74 | 75 | # Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Server 76 | conn = Db2ConnectionMgr('SERVER', '', hostName, portNum, userID, passWord) 77 | conn.openConnection() 78 | if conn.returnCode is True: 79 | svrConnection = conn.connectionID 80 | else: 81 | conn.closeConnection() 82 | exit(-1) 83 | 84 | # Attempt To Create A New Database At The Remote Server 85 | print("Creating a database named " + dbName + " at the " + hostName + " server. Please wait.") 86 | try: 87 | returnCode = ibm_db.createdbNX(svrConnection, dbName) 88 | except Exception: 89 | pass 90 | 91 | # If The Database Could Not Be Created, Display An Error Message And Exit 92 | if returnCode is False: 93 | print("ERROR: Unable to create the " + dbName + " database.\n") 94 | errorMsg = ibm_db.conn_errormsg(svrConnection) 95 | print(errorMsg + "\n") 96 | conn.closeConnection() 97 | exit(-1) 98 | 99 | # Otherwise, Display A Status Message And Verify That Information About The Database 100 | # That Was Just Created Exists In The Db2 System Database Directory 101 | else: 102 | print("\nThe database \"" + dbName + "\" has been created!\n") 103 | query_sdb_dir(dbName) 104 | 105 | # Close The Db2 Server Connection That Was Opened Earlier 106 | conn.closeConnection() 107 | 108 | # Return Control To The Operating System 109 | exit() 110 | -------------------------------------------------------------------------------- /Python_Examples/ibm_db_dbi/ibm_db_dbi-fetchone.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3 2 | #-------------------------------------------------------------------------------------------------# 3 | # NAME: ibm_db_dbi-fetchone.py # 4 | # # 5 | # PURPOSE: This program is designed to illustrate how to use the .fetchone() function of the # 6 | # cursor object associated with the IBM_DBConnection object returned by the # 7 | # ibm_db_dbi.connect() API. # 8 | # # 9 | # USAGE: Log in as a Db2 database instance user (for example, db2inst1) and issue the # 10 | # following command from a terminal window: # 11 | # # 12 | # ./ibm_db_dbi-fetchone.py # 13 | # # 14 | #-------------------------------------------------------------------------------------------------# 15 | # DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY # 16 | # # 17 | # (C) COPYRIGHT International Business Machines Corp. 2018, 2019 All Rights Reserved # 18 | # Licensed Materials - Property of IBM # 19 | # # 20 | # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP # 21 | # Schedule Contract with IBM Corp. # 22 | # # 23 | # The following source code ("Sample") is owned by International Business Machines Corporation # 24 | # or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, # 25 | # copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose # 26 | # of assisting you in the creation of Python applications using the ibm_db_dbi library. # 27 | # # 28 | # The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM # 29 | # HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT # 30 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # 31 | # Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the # 32 | # above limitations or exclusions may not apply to you. IBM shall not be liable for any damages # 33 | # you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM # 34 | # has been advised of the possibility of such damages. # 35 | #-------------------------------------------------------------------------------------------------# 36 | 37 | # Load The Appropriate Python Modules 38 | import sys # Provides Information About Python Interpreter Constants, Functions, & Methods 39 | import ibm_db_dbi # Contains The APIs Needed To Work With Db2 Databases 40 | 41 | # Define And Initialize The Appropriate Variables 42 | dbName = "SAMPLE" # The Alias For The Cataloged, Local Database 43 | userID = "db2inst1" # The Instance User ID At The Local Server 44 | passWord = "Passw0rd" # The Password For The Instance User ID At The Local Server 45 | connectionID = None 46 | resultSet = False 47 | returnCode = False 48 | 49 | # Display A Status Message Indicating An Attempt To Establish A Connection To A Db2 Database 50 | # Is About To Be Made 51 | print("\nConnecting to the \'" + dbName + "\' database ... ", end="") 52 | 53 | # Construct The String That Will Be Used To Establish A Db2 Database Connection 54 | connString = "ATTACH=FALSE" # Attach To A Database; Not A Server 55 | connString += ";DATABASE=" + dbName # Required To Connect To A Database 56 | connString += ";PROTOCOL=TCPIP" 57 | connString += ";UID=" + userID 58 | connString += ";PWD=" + passWord 59 | 60 | # Attempt To Establish A Connection To The Database Specified 61 | try: 62 | connectionID = ibm_db_dbi.connect(connString, '', '') 63 | except Exception: 64 | pass 65 | 66 | # If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit 67 | if connectionID is None: 68 | print("\nERROR: Unable to connect to the \'" + dbName + "\' database.") 69 | print("Connection string used: " + connString + "\n") 70 | exit(-1) 71 | 72 | # Otherwise, Complete The Status Message 73 | else: 74 | print("Done!\n") 75 | 76 | # Retrieve The Cursor Object That Was Created For The Connection Object 77 | if not connectionID is None: 78 | cursorID = connectionID.cursor() 79 | 80 | # Define The SQL Statement That Is To Be Used To Retrieve Data From The PROJECT Table 81 | sqlStatement = "SELECT projno, projname FROM project WHERE prstaff < 2" 82 | 83 | # Execute The SQL Statement Just Defined 84 | print("Executing the SQL statement \"" + sqlStatement + "\" ... ", end="") 85 | try: 86 | resultSet = cursorID.execute(sqlStatement) 87 | except Exception: 88 | pass 89 | 90 | # If The SQL Statement Could Not Be Executed, Display An Error Message And Exit 91 | if resultSet is False: 92 | print("\nERROR: Unable to execute the SQL statement specified.\n") 93 | connectionID.close() 94 | exit(-1) 95 | 96 | # Otherwise, Complete The Status Message 97 | else: 98 | print("Done!\n") 99 | 100 | # Display A Report Header 101 | print("Query results:\n") 102 | print("PROJNO PROJNAME") 103 | print("______ _____________________") 104 | 105 | # As Long As There Are Records In The Result Set Produced, ... 106 | noData = False 107 | while noData is False: 108 | 109 | # Retrieve A Record And Store It In A Python Tuple 110 | try: 111 | resultSet = cursorID.fetchone() 112 | except: 113 | pass 114 | 115 | # If The Data Could Not Be Retrieved Or There Was No Data To Retrieve, Set The 116 | # "No Data" Flag And Exit The Loop 117 | if resultSet is None: 118 | noData = True 119 | 120 | # Otherwise, Display The Data Retrieved 121 | else: 122 | print("{:<6} {:24}" .format(resultSet[0], resultSet[1])) 123 | 124 | # Add A Blank Line To The End Of The Report 125 | print() 126 | 127 | # Attempt To Close The Db2 Database Connection That Was Opened Earlier 128 | if not connectionID is None: 129 | print("Disconnecting from the \'" + dbName + "\' database ... ", end="") 130 | try: 131 | returnCode = connectionID.close() 132 | except Exception: 133 | pass 134 | 135 | # If The Db2 Database Connection Was Not Closed, Display An Error Message And Exit 136 | if returnCode is False: 137 | print("\nERROR: Unable to disconnect from the " + dbName + " database.") 138 | exit(-1) 139 | 140 | # Otherwise, Complete The Status Message 141 | else: 142 | print("Done!\n") 143 | 144 | # Return Control To The Operating System 145 | exit() 146 | --------------------------------------------------------------------------------