You can try using DictCursor. This is not standard API 2.0.
DictCursor provides a dictionary interface, keys are "column" or "table.column" if there are two columns with the same name.
def query_db(query, args=(), one=False):
cur = MySQLdb.cursors.DictCursor(db)
cur.execute(query, args)
rv = [row for row in cur]
return (rv[0] if rv else None) if one else rv
Nevertheless I suggets to use a workaround in SQL using AS
for specify alias for columns in your select
. For example:
select table1.name as `table1.name`, table2.name as `table2.name`
from table1 inner join table2
on table1.id = table2.id
...
>>> cur.description
(('table1.name', 253, 32, 32, 32, 0, 0), ('table2.name', 253, 18, 80, 80, 0, 0))