#!/usr/bin/env python # -*- coding:utf-8 -*- d = {‘name‘: ‘alx‘, ‘age‘: 18,‘k1‘:123,‘k2‘: 123} t = "inset into tb(%s) values(%s)" key_list =[] value_list = [] for k,v in d.items(): key_list.append(k) value_list.append("%%(%s)s" %k) sql = t %( ‘,‘.join(key_list), ‘,‘.join(value_list)) print(sql)
inset into tb(k2,k1,name,age) values(%(k2)s,%(k1)s,%(name)s,%(age)s), 字典
def add_merchant(self, **kwargs): cursor = self.db_conn.connect() sql = """insert into merchant(%s) values(%s)""" key_list = [] value_list = [] for k, v in kwargs.items(): key_list.append(k) value_list.append(‘%%(%s)s‘ % k) sql = sql % (‘,‘.join(key_list), ‘,‘.join(value_list)) cursor.execute(sql, kwargs) self.db_conn.close()
时间: 2024-11-07 14:55:40