import hashlib
import pymysql


# sqlcreate = """CREATE TABLE md5 (
#          ID  BIGINT NOT NULL,
#          MD5  CHAR(128) )"""
# sqlinsert = """INSERT INTO md5(ID,
#          MD5)
#          VALUES (96, 'b0f2a038c3be5306dafa596156b8b359')"""


m = hashlib.md5()
db=pymysql.connect("ip","数据库名","数据库密码","数据库表名")
for i in range(1,1000000):
    cursor=db.cursor()
    m.update(str(i).encode(encoding='UTF-8'))
    sqlinsert1 = """INSERT INTO md5(ID,
         MD5)
         VALUES ({id_}, '{md5_}')""".format(id_=i ,md5_= m.hexdigest())
    try:
        cursor.execute(sqlinsert1)
        print(str(i)+",")
        db.commit()
    except:
        db.rollback()
db.close()