简体   繁体   中英

How do I write data to a file that I have taken from an SQL database in Python? (sqlite3)

c.execute("SELECT * FROM Student WHERE Forename =:forename AND Surname =:surname AND YearGroup =:yeargroup AND FormNumber =:formnumber BETWEEN :startdate AND :enddate", {"forename": forename, "surname": surname, "yeargroup": yeargroup, "formnumber": formnumber, "startdate": startdate, "enddate": enddate})

studentRequest = c.fetchall()
records = studentRequest

#write to file

f = open('Student Report', 'w')
f.write(name)
f.write(' Form Number: ' + (yeargroup +'/'+ formnumber))
f.write(str(records))
f.close()

This is my code but it doesn't seem to write the data from the database to the file. The name, Forum Number etc is written though.

Can anyone help?

Does your output have "[]" in it? It sounds like your query doesn't find any matches.

If this isn't so, prepare a complete, runnable example showing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM