#!/usr/bin/env python import MySQLdb import sys import fileinput devdb_host = "10.10.1.1" prdb_host = "10.10.2.2" prdb = MySQLdb.connect(prdb_host, "angcoup", "impasswd", "mydb") devdb = MySQLdb.connect(devdb_host, "angcoup", "impasswd", "mydb") prdb_cursor = prdb.cursor() devdb_cursor = devdb.cursor() columnsSql = "col1, col2, col3" selectSql = "SELECT " + columnsSql + " from angcoup.angcoups WHERE angcoup_srl={0}" insertSql = "replace into angcoup.angcoups ( " + columnsSql + " ) values ('{0}', '{1}', '{2}') for angcoupSrl in fileinput.input(): selectSqlBinded = selectSql.format(angcoupSrl) print selectSqlBinded prdb_cursor.execute(selectSqlBinded) r = prdb_cursor.fetchone(); insertSqlBinded = insertSql.format(r[0], r[1], r[2]) print insertSqlBinded devdb_cursor.execute(insertSqlBinded) #devdb_cursor.execute("select last_insert_id()"); devdb.commit() sys.stdout.write("inserted"); print "Done." devdb.close() prdb.close()