aiomysql has DictCursor

This commit is contained in:
Tadas Talaikis 2017-05-27 11:06:45 +03:00 committed by GitHub
parent 21aa3f6578
commit a5249d1f5d

View File

@ -46,15 +46,13 @@ async def get_pool(app, loop):
@app.route("/") @app.route("/")
async def test(): async def test():
result = []
data = {} data = {}
async with app.pool['aiomysql'].acquire() as conn: async with app.pool['aiomysql'].acquire() as conn:
async with conn.cursor() as cur: async with conn.cursor(aiomysql.DictCursor) as cur:
await cur.execute("SELECT question, pub_date FROM sanic_polls") await cur.execute("SELECT question, pub_date FROM sanic_polls")
async for row in cur: result = await cur.fetchall()
result.append({"question": row[0], "pub_date": row[1]})
if result or len(result) > 0: if result or len(result) > 0:
data['data'] = res data['data'] = result
return json(data) return json(data)