import pymysql conn = pymysql.connect(host="localhost", port=3306, user="root", password="root", database="hello") cursor = conn.cursor() for i in range(1, 20): sql_r = "insert into ticket (sn,price,date) value ('{}','{}','{}')" sn = str(i) if len(sn) < 6: # 追加0前缀 sn = "0" * (6 - len(sn)) + sn sql = sql_r.format(sn, 88 + i, "2022-02-18") cursor.execute(sql)