I have written some python code to connect to a MySQL database, open a text file and, for each entry in the text file, execute a query and write the result to an output file. Rather than writing the result of every query to the output file, however only a single result is being written. If the query I'm using has no arguments it all works fine. The issue only arises when I try to add in arguments.
I'm fairly new to Python so I may be making a silly mistake but I haven't yet been able to come across anything that's helped so any assistance would be greatly appreciated.
The code I have is:
output = open("results.txt", "w+")
cnx= mysql.connector.connect(**config)
cursor = cnx.cursor()
with open("input.txt") as i:
for line in i:
# Construct query to get type
query = ("select type from table1, table2 "
"where table1.name = %s and table1.id = table2.id")
# Query arguments
args = (line)
cursor.execute(query, args) # Execute query
for entry in cursor:
output.write(str(entry) + "\n")
cursor.close()
cnx.close()
output.close()? If not, maybe the data is lost in the output buffer.