Using python to write mysql query to csv, need to show field names

Solution 1:

You can dump all results to the csv file without looping:

rows = cursor.fetchall()
fp = open('/tmp/file.csv', 'w')
myFile = csv.writer(fp)
myFile.writerows(rows)
fp.close()

Solution 2:

result is a list of rows. So you'll need to iterate through that list and write each row:

for row in result:
    c.writerow(row)