import random import MySQLdb def prepare_data(): result = [] type = ["a", "b", "c", "d", "e"] for i in range(50000): index = random.randint(0, 4) result.append((str(i), str(type[index]), str(i + 10), str(i))) return result def insert_data(): db = MySQLdb.connect(host='10.124.207.xxx', user='xxxx', passwd='xxxxx', db='usecase', charset='utf8') sql = 'INSERT INTO test_table ( name, type, phone, addr) VALUES ( %s, %s, %s, %s);' cur = db.cursor() cur.executemany(sql, prepare_data()) db.commit() db.close() if __name__ == '__main__': try: insert_data() except Exception as e: