代码拉取完成,页面将自动刷新
__author__ = 'thanatos'
import logging; logging.basicConfig(level=logging.INFO)
import asyncio, aiomysql
@asyncio.coroutine
def create_pool(loop, **kw):
logging.info('create database connection pool...')
global __pool
__pool = yield from aiomysql.create_pool(
host=kw.get('host', 'localhost'),
port=kw.get('port', 3306),
user=kw['user'],
password=kw['password'],
db=kw['db'],
charset=kw.get('charset', 'utf8'),
autocommit=kw.get('autocommit', True),
maxsize=kw.get('maxsize', 10),
minsize=kw.get('minsize', 1),
loop=loop
)
@asyncio.coroutine
def select(sql, args, size=None):
global __pool
with (yield from __pool) as conn:
cur = yield from conn.cursor(aiomysql.DictCursor)
yield from cur.execute(sql.replace('?', '%s'), args or ())
if size:
rs = yield from cur.fetchmany(size)
else:
rs = yield from cur.fetchall()
yield from cur.close()
logging.info('rows returned: %s' % len(rs))
return rs
@asyncio.coroutine
def execute(sql, args, is_insert=False):
with (yield from __pool) as conn:
try:
cur = yield from conn.cursor()
logging.info('update sql is %s' % sql)
yield from cur.execute(sql.replace('?', '%s'), args)
yield from cur.close()
except BaseException as e:
raise e
return cur.rowcount if not is_insert else cur.lastrowid
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。