读取一个mysql表中的数据,并且将其存储为log数据文件
import pymysql
import os
connection=pymysql.connect(host='XXXXX',
user='XXXX',
password='XXXXX',
db='XXXX',
port=3306,
charset='utf8')
cursor=connection.cursor()
sql="select DISTINCT cmptId from time_checkinrecord_bak20180605"
cursor.execute(sql)
cmptIdList=cursor.fetchall()
for cmptId in cmptIdList:
cmptidStr=str(cmptId[0])
print(cmptidStr)
file_name=os.path.join('D:\logs','cmpt-'+cmptidStr+'.log')
score_open=open(file_name,'w',encoding='utf-8')
scoreSql = "select cmptId,markNo,cinTime,cinType,cpId from time_checkinrecord_bak20180605 where cmptId=" + cmptidStr
cursor.execute(scoreSql)
scoreList = cursor.fetchall()
for score in scoreList:
score_open.write(str(score[1])+","+str(score[2])+","+str(score[3])+","+str(score[4])+"\n")
score_open.close()
cursor.close()