Read database entries from mysql in the form of dictionary in python
Question: > **Possible Duplicate:**  
>  [Python: use mysqldb to import a MySQL table as a
> dictionary?](http://stackoverflow.com/questions/2180226/python-use-mysqldb-
> to-import-a-mysql-table-as-a-dictionary)

I currently get the db entry in the form of array using the commands

    
    
    db_conn = mdb.connect(host = "localhost", user = "username", passwd = "password", db = "db_name", charset='utf8')
    cursor = db_conn.cursor()
    cursor.execute(sql_query)
    rows = cursor.fetchall()
    

I get the row values in the form of array. Can I get in the form of dictionary
with column names as keys. I couldn't find any such command.


Answer: Set the cursor factory when you connect:

    
    
    db_conn = mdb.connect(
        host="localhost", user="username", passwd="password", db="db_name",
        charset='utf8', cursorclass=mdb.cursors.DictCursor)
    

