python 字典生成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))

 

posted @ 2020-01-31 14:43  酷酷的城池  阅读(473)  评论(0编辑  收藏  举报