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)
人生的旅途,前途很远,也很暗。然而不要怕,不怕的人的面前才有路。