python 字典直接生成sql语句
shopInfo = {}
shopInfo['name']='1'
shopInfo['address']='1'
shopInfo['city']='1'
shopInfo['phone']='1'
shopInfo['number']='1'
sql = "insert into 表明(%s) values(%s)"
key = ", ".join('`{}`'.format(k) for k in shopInfo.keys())
val = ', '.join('"{{{}}}"'.format(k) for k in shopInfo.keys())
sql = (sql % (key,val))+'format(**shopInfo)'
print(sql)
# 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))