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))

 

上一篇:用 Python制作一盏 3D 花灯,喜迎元宵佳节


下一篇:C++模版类实现稀疏矩阵(转置运算和快速转置运算)