Python, MySQL and cursors that fetch maps -
this question has answer here:
after executing query statement on mysql database connection, perform:
rows = cursor.fetchall()
this gives array of arrays. i'd have array of dictionaries, each dictionary takes keys requested column names of table , associates values table.
how do this?
well, forgot mention mysql library you're using.
if using oursql (which recommend, best one), use oursql's
dictcursor
. example:conn = oursql.connect(...) curs = conn.cursor(oursql.dictcursor)
if using mysqldb (why?) use mysqldb's
dictcursor
. example:conn = mysqldb.connect(..., cursorclass=mysqldb.cursors.dictcursor) curs = conn.cursor()
doing give cursor returns dicts each row. remember not have duplicate rownames in query.
Comments
Post a Comment