data = { ‘id‘: ‘1‘, ‘name‘: ‘zengsf‘, ‘age‘: 20 } table = ‘students‘ #获取到一个以键且为逗号分隔的字符串,返回一个字符串 keys = ‘, ‘.join(data.keys()) values = ‘, ‘.join([‘%s‘] * len(data)) sql = ‘INSERT INTO {table}({keys}) VALUES ({values})‘.format(table=table, keys=keys, values=values) try: #这里的第二个参数传入的要是一个元组 if cursor.execute(sql, tuple(data.values())): print(‘Successful‘) db.commit() except: print(‘Failed‘) db.rollback() db.close()
参考地址:https://cuiqingcai.com/5578.html
原文地址:https://www.cnblogs.com/zengsf/p/9734476.html
时间: 2024-10-15 10:01:03