Insert, Update, Delete
要执行INSERT、UPDATE、DELETE语句,可以定义一个通用的 execute() 函数,因为这3种SQL的执行都需要相同的参数,以及返回一个整数表示影响的行数:
@asyncio.coroutine
def execute(sql, args):
log(sql)
with (yield from __pool) as conn:
try:
cur = yield from conn.cursor()
yield from cur.execute(sql.replace('?', '%s'), args)
affected = cur.rowcount
yield from cur.close()
except BaseException as e:
raise
return affected
execute() 函数和 select() 函数所不同的是,cursor对象不返回结果集,而是通过 rowcount 返回结果数。