加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dao.py 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
mxxim 提交于 2015-07-01 16:08 . 。。
__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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化