0
import mysql.connector

mydb = mysql.connector.connect(host="localhost", user="", passwd="", db="world")
cursor = mydb.cursor()

file = open('C:\\test\\File.txt', 'w')
file_content = file.write()


query = "SELECT * FROM city where name='kabul'"

cursor.execute(query, (file_content,))

mydb.commit()
mydb.close()

Here i'm storing the data in text file but error name 'file_content' is not defined

6
  • You probably want to do file.read(), seeing as write expects a parameter of data to write. And you probably want open('...', 'r') and not 'w' seeing as that overwrites the file, rather than opening it for data-reading. Commented Feb 25, 2019 at 9:29
  • Can you suggest me to store the database data in text file Commented Feb 25, 2019 at 9:33
  • I don't understand what you're trying to do. Are you trying to store the response from the database in the file? If so you should write to the file and not just pass it as argument. If you're trying to read parameters for the query from the file then you have to use file.read. Although, the query has no paramters so it makes no sense as well. Please clarify. Commented Feb 25, 2019 at 9:33
  • I think you are looking for something like this stackoverflow.com/questions/21270148/….. posted code will not work anyways Commented Feb 25, 2019 at 9:36
  • I would suggest you re-write the entire thing and re-learn everything. You've missunderstood the basics of working with files and working with database calls. You've flipped the things around completely. So start over by reading and writing files, get a hang for it. Re-learn simple database inputs and outputs, how to store and how to fetch results. Once you're comfortable with these two, merge them together into your solution. Commented Feb 25, 2019 at 9:39

1 Answer 1

1

I assume that you want fetch all the rows from MySQL and then further write the row-data in the file.

import mysql.connector

mydb = mysql.connector.connect(host="localhost", user="", passwd="", db="world")
cursor = mydb.cursor()

file_to_use = open('C:\\test\\File.txt', 'w+')

query = "SELECT * FROM city where name='kabul'"

cursor.execute(query)
row_data = cursor.fetchone()
while row_data is not None:
    file_to_use.write(str(row_data))
    file_to_use.write("\n")
    row_data = cursor.fetchone()
file_to_use.close()
mydb.commit()
mydb.close()

This will write the fetched tuple which is row_data into the text file with each row in a new line.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. but i'm getting this error >>> row_data = cursor.fetchone() >>> while row_data is not None: ... file.write(str(row_data)) ... file.write("\n") ... row_data = cursor.fetchone() ... file.close() File "<stdin>", line 5 file.close() ^ SyntaxError: invalid syntax
Well, there must be an error in your code where file.close() is not written the way it is intended to. Also, I'd ask you to change the variable name from file to something like new_file or file_to_use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.