Tornado中insert into sql语句的构造

#!/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()

 

posted @ 2017-04-30 11:03  失落的黎明  阅读(357)  评论(0编辑  收藏  举报