0

Possible Duplicate:
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.

Community
  • 1
  • 1
raju
  • 4,788
  • 15
  • 64
  • 119

1 Answers1

2

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343