Exemplo n.º 1
0
    def run_test_recreateDB(self):
        database = 'temo'
        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:
                # call recreatedb with no codeset argument when specified database not exeist
                rc = ibm_db.recreatedb(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 occure during create database')

                conn = ibm_db.connect(conn_str, '', '')
                if conn:
                    ibm_db.close(conn)
                    conn = False
                    # call recreate db with codeset argument when specified database  exist
                    rc = ibm_db.recreatedb(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('Error occure during recreate db with codeset')

                #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())
Exemplo n.º 2
0
    def run_test_recreateDB(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:        
                # call recreatedb with no codeset argument when specified database not exeist   
                rc = ibm_db.recreatedb(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 occure during create database'

                conn = ibm_db.connect(conn_str, '', '')
                if conn:
                    ibm_db.close(conn)
                    conn = False
                    # call recreate db with codeset argument when specified database  exist
                    rc = ibm_db.recreatedb(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 'Error occure during recreate db with codeset'
                            
                #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 The Local Db2 Server
conn = Db2ConnectionMgr('LOCAL_SVR', '', '', '', userID, passWord)
conn.openConnection()
if conn.returnCode is True:
    svrConnection = conn.connectionID
else:
    conn.closeConnection()
    exit(-1)

# Attempt To Drop And Recreate A Database At The Local Server
print("Dropping and recreating a database named " + dbName + ". Please wait.")
try:
    returnCode = ibm_db.recreatedb(svrConnection, dbName, 'UTF-8')
except Exception:
    pass

# If The Database Could Not Be Recreated, Display An Error Message And Exit 
if returnCode is False:
    print("ERROR: Unable to drop and recreate the " + dbName + " database.\n")
    errorMsg = ibm_db.conn_errormsg(svrConnection)
    print(errorMsg + "\n")

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