cols = ", ".join('`{}`'.format(k) for k in i.keys()) val_cols = ', '.join('%({})s'.format(k) for k in i.keys()) inst = ''' INSERT INTO `baidu` (%s) VALUES(%s); ''' res_sql = inst % (cols, val_cols) try: cursor.execute(res_sql, i) except MySQLdb._exceptions.IntegrityError: print('零件主键重复'+str(id))