python连接mysql数据表查询表获取数据导入到txt中

import pymysql
'''连接mysql数据表查询表获取数据导入到txt中'''
#查询结果写入数据到txt
def get_loan_number(file_txt):
connect = pymysql.Connect(
host="100.100.100.0",
port=3306,
user="test",
passwd="123",
db="bigdata",
charset='utf8'
)
print("写入中,请等待……")
cursor = connect.cursor()
sql = "select * from ts_vehicle_load"
cursor.execute(sql)
number = cursor.fetchall()
fp = open(file_txt, "w")
loan_count = 0
for loanNumber in number:
loan_count += 1
fp.write(str(loanNumber) + "\n")
fp.close()
cursor.close()
connect.close()
print("写入完成,共写入%d条数据!" % loan_count)


if __name__ == "__main__":
file_txt = r"E:\Users\admin\PycharmProjects\untitled\d"
get_loan_number(file_txt)






posted @ 2019-06-12 15:19  暮色森森  阅读(2944)  评论(0编辑  收藏  举报