使用python将数据库中的student表中提取数据写入到db.txt

import pymysql

def get_file_down(file):
    connect = pymysql.Connect(
        host = "127.0.0.1",
        port = 3306,
        user = "root",
        passwd = "123",
        db = "mymessage",
        charset='utf8'
    )
    print('等待写入中....')
    cursor = connect.cursor()
    sql = "select * from student"
    cursor.execute(sql)
    numbers = cursor.fetchall()
    fp = open('file','w')
    loan_count = 0
    for number in numbers:
        loan_count += 1
        fp.write(str(number)+"\n")
    fp.close()
    cursor.close()
    connect.close()
    print("写入完成,一共完成了{}数据".format(loan_count))


if __name__ == '__main__':
    file = r"C:\Users\1\Desktop\my.txt"
    get_file_down(file)

  通过上述操作,就可以实现了!

posted on 2022-10-17 16:12  一先生94  阅读(164)  评论(0编辑  收藏  举报

导航