Friday, November 19, 2010

Fatching SQL table as python dictionary

'''
tableName:
a        b
--------------
as      uu
'''
conn = MySQLdb.connect (host = "localhost",
                           user = "root",
                           passwd = "",
                           db = "dbName",
                           cursorclass=MySQLdb.cursors.DictCursor
                           )

cursorDict = conn.cursor()
cursorDict.execute("SELECT * FROM tableName")
rowDict = cursorDict.fetchone()

print rowDict

# this will print {"a":"as", "b":"uu"}

No comments:

Post a Comment