import pymysql
from pymysql.cursors import DictCursor
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', charset='utf8', database='moms')
cursor = conn.cursor(cursor=DictCursor)
sql = "select * from class_demo"
cursor.execute(sql)
data_list = cursor.fetchall()
print(data_list)
for item in data_list:
line = "{},{},{}\n".format(item["name"],item['age'],item['class_name'])
with open('accunt.txt', mode='a', encoding='utf-8') as f:
f.write(line)
cursor.close()
conn.close()