예제 #1
0
    def run_test_createDropDB(self):
        database = 'test001'
        conn_str = "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % (database, config.hostname, config.port, config.user, config.password)
        conn_str_attach = "attach=true;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % (config.hostname, config.port, config.user, config.password) #for create db or drop db API it is nessesory that connection only attach to the DB server not to any existing database of DB server
        conn_attach = ibm_db.connect(conn_str_attach, '', '')

        if conn_attach:
            conn = False
            try:
                conn = ibm_db.connect(conn_str, '', '')
            except:
                pass

            if conn:
                ibm_db.close(conn)
                conn = False
                try:
                    ibm_db.dropdb(conn_attach, database)
                except:
                    print('Errors occurred during drop database')
            try:
                #create databse
                rc = ibm_db.createdb(conn_attach, database)
                if rc:
                    conn = ibm_db.connect(conn_str, '', '')
                    if conn:
                        print('database created sucessfully')
                        ibm_db.close(conn)
                        conn = False
                    else:
                        print('database is not created')
                else:
                    print('Errors occurred during create database')

                #drop databse
                rc = ibm_db.dropdb(conn_attach, database)
                if rc:
                    try:
                        conn = ibm_db.connect(conn_str, '', '')
                    except:
                        print('datbase droped sucessfully')
                    if conn:
                        print('Errors occurred during drop database')
                        ibm_db.close(conn)
                        conn = False
                else:
                    print('Errors occurred during delete database')

                #create database with codeset option
                rc = ibm_db.createdb(conn_attach, database, 'iso88591')
                if rc:
                    conn = ibm_db.connect(conn_str, '', '')
                    server_info = ibm_db.server_info( conn )
                    if conn and (server_info.DB_CODEPAGE == 819):
                        print('database with codeset created sucessfully')
                        ibm_db.close(conn)
                        conn = False
                    else:
                        print('database is not created')
                else:
                    print('Errors occurred during create database')

                #drop database
                rc = ibm_db.dropdb(conn_attach, database)
                if rc:
                    try:
                        conn = ibm_db.connect(conn_str, '', '')
                    except:
                        print('datbase droped sucessfully')
                    if conn:
                        print('Errors occurred during drop database')
                        ibm_db.close(conn)
                        conn = False
                else:
                    print('Errors occurred during drop database')
            except:
                print(ibm_db.conn_errormsg())
                pass
            ibm_db.close(conn_attach)
        else:
            print(ibm_db.conn_errormsg())
예제 #2
0
    def run_test_createDropDB(self):
        database = "test001"
        conn_str = "DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % (
            database,
            config.hostname,
            config.port,
            config.user,
            config.password,
        )
        conn_str_attach = "attach=true;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;" % (
            config.hostname,
            config.port,
            config.user,
            config.password,
        )  # for create db or drop db API it is nessesory that connection only attach to the DB server not to any existing database of DB server
        conn_attach = ibm_db.connect(conn_str_attach, "", "")

        if conn_attach:
            conn = False
            try:
                conn = ibm_db.connect(conn_str, "", "")
            except:
                pass

            if conn:
                ibm_db.close(conn)
                conn = False
                try:
                    ibm_db.dropdb(conn_attach, database)
                except:
                    print "Errors occurred during drop database"
            try:
                # create databse
                rc = ibm_db.createdb(conn_attach, database)
                if rc:
                    conn = ibm_db.connect(conn_str, "", "")
                    if conn:
                        print "database created sucessfully"
                        ibm_db.close(conn)
                        conn = False
                    else:
                        print "database is not created"
                else:
                    print "Errors occurred during create database"

                # drop databse
                rc = ibm_db.dropdb(conn_attach, database)
                if rc:
                    try:
                        conn = ibm_db.connect(conn_str, "", "")
                    except:
                        print "datbase droped sucessfully"
                    if conn:
                        print "Errors occurred during drop database"
                        ibm_db.close(conn)
                        conn = False
                else:
                    print "Errors occurred during delete database"

                # create database with codeset option
                rc = ibm_db.createdb(conn_attach, database, "iso88591")
                if rc:
                    conn = ibm_db.connect(conn_str, "", "")
                    server_info = ibm_db.server_info(conn)
                    if conn and (server_info.DB_CODEPAGE == 819):
                        print "database with codeset created sucessfully"
                        ibm_db.close(conn)
                        conn = False
                    else:
                        print "database is not created"
                else:
                    print "Errors occurred during create database"

                # drop database
                rc = ibm_db.dropdb(conn_attach, database)
                if rc:
                    try:
                        conn = ibm_db.connect(conn_str, "", "")
                    except:
                        print "datbase droped sucessfully"
                    if conn:
                        print "Errors occurred during drop database"
                        ibm_db.close(conn)
                        conn = False
                else:
                    print "Errors occurred during drop database"
            except:
                print ibm_db.conn_errormsg()
                pass
            ibm_db.close(conn_attach)
        else:
            print ibm_db.conn_errormsg()
dbName = "MY_DB"
returnCode = False

# Create An Instance Of The Db2ConnectionMgr Class And Use It To Connect To A Db2 Server
conn = Db2ConnectionMgr('SERVER', '', hostName, portNum, userID, passWord)
conn.openConnection()
if conn.returnCode is True:
    svrConnection = conn.connectionID
else:
    conn.closeConnection()
    exit(-1)

# Attempt To Create A New Database At The Remote Server
print("Creating a database named " + dbName + " at the " + hostName + " server. Please wait.")
try:
    returnCode = ibm_db.createdb(svrConnection, dbName)
except Exception:
    pass

# If The Database Could Not Be Created, Display An Error Message And Exit 
if returnCode is False:
    print("ERROR: Unable to create the " + dbName + " database.\n")
    errorMsg = ibm_db.conn_errormsg(svrConnection)
    print(errorMsg + "\n")
    conn.closeConnection()
    exit(-1)

# Otherwise, Display A Status Message And Verify That Information About The Database
# That Was Just Created Exists In The Db2 System Database Directory
else:
    print("\nThe database \"" + dbName + "\" has been created!\n")