omnilib/aiosqlite

How can I get named columns in rows?

antonkomarev opened this issue · 2 comments

Description

Is there is a way to get named rows in result?

query = """
    SELECT name
    FROM game
    WHERE id = :id
"""
parameters = {
    "id": game_id,
}
async with self.db_connection.execute(query, parameters) as cursor:
    result = await cursor.fetchone()

    if not result:
        return None

    game_name = result["game_name"]

print(result['game_name'])
TypeError: tuple indices must be integers or slices, not str

Right now I can only get retrieved values only by column index :\

    game_name = result[0]

Details

  • OS: OS X
  • Python version: 3.6
  • aiosqlite version: 0.17.0
  • Can you repro on 'main' branch? Yes
  • Can you repro in a clean virtualenv? Yes

Same way as you would with the stdlib sqlite3 module: set the .row_factory attribute on the connection object to aiosqlite.Row or sqlite3.Row. See https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory for details.

Thank you for quick detailed response!