Пример #1
0
connString = "ATTACH=FALSE"  # Attach To A Database; Not A Server
connString += ";DATABASE=" + dbName  # Required To Connect To A Database
connString += ";PROTOCOL=TCPIP"
connString += ";UID=" + userID
connString += ";PWD=" + passWord

# Attempt To Establish A Connection To The Database Specified
try:
    dbConnection = ibm_db.connect(connString, '', '')
except Exception:
    pass

# If A Db2 Database Connection Could Not Be Established, Display An Error Message And Exit
if dbConnection is None:
    print("\nERROR: Unable to connect to the \'" + dbName + "\' database.")
    exit(-1)

# Otherwise, Complete The Status Message
else:
    print("Done!\n")

# Add Additional Db2 Database-Related Processing Here ...

# Attempt To Close The Db2 Database Connection That Was Just Opened
if not dbConnection is None:
    print("Disconnecting from the \'" + dbName + "\' database ... ", end="")
    try:
        returnCode = ibm_db.close(dbConnection)
    except Exception:
        pass
      "\' server ... \n")

# Attempt To Close All Of The Remaining Db2 Server Connections That Were Opened Earlier
for loopCounter in range(10):

    # If The Specified Connection Is Open, Attempt To Close It
    if not svrConnection[loopCounter] is None:
        try:
            returnCode = ibm_db.close(svrConnection[loopCounter])
        except Exception:
            pass

        # If The Connection Could Not Be Closed, Display An Error Message And Continue
        if returnCode is False:
            print("\nERROR: Unable to disconnect from the " + hostName +
                  " server.")
            continue

        # Otherwise, Display A "Connection Closed" Status Message
        else:
            print("  Connection {:>2} closed!".format(loopCounter + 1))

# Display A Status Message Indicating All Db2 Server Connections Have Been Returned To The
# Connection Pool
print(
    "\nAll Db2 server connections have been returned the pool of connections opened earlier.\n"
)

# Return Control To The Operating System
exit()