Skip to content

Example Code Snippets #3

@mkeneqa

Description

@mkeneqa

Migrating Data from one Database Table to Another

Note: The destination Table Schema should be the same as the source

def MigrateSourceTableToDestTable():

    source_db = dbclass.Database(the_host=10.10.10.123,the_db='my_source_db')
    dest_db = dbclass.Database(the_host=10.10.10.125, the_db='my_dest_db')
    _the_table = 'table_to_migrate'

    _qry = "SELECT * FROM {}.{}".format(source_db, the_table)
    rows = source_db.GetUnbuffQryCursorObject(_qry)

    for row in rows.fetchall_unbuffered():
        col_val = iter(row)
        next(col_val)  # skip the id as dest will auto increment

        _record = {
            "`col_1`": str(next(col_val)),
            "`col_2`": str(next(col_val)),
            "`col_3`": str(next(col_val)),
            "`col_4`": str(next(col_val)),
            "`col_5`": str(next(col_val))
        }

        dest_db.InsertDictionaryValues(db_table=the_table, insert_dict=_record, verbose_print=True)

    source_db.CloseConn()
    dest_db.CloseConn()

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions