how to get column names in python mysql/sql query
Saturday, May 17th, 2008First obtain a cursor
import MySQLdb
conn = MySQLdb.connect (host = “localhost”, user = “root”, passwd = “”, db = “dbname”)
cursor = conn.cursor()
Now execute mysql sql query
sql = “select COLA, COLB from T1 where COLC=%”
colc_value = “something”
cursor.execute(sql, [colc_value])
Now iterate over results
while(1):
data = cursor.fetchone()
if data == None: break
desc = cursor.description
dict = {}
for (name, value) in zip(desc, data):
dict[str(name[0])] = value
print dict