-1
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

Naresh
  • 16,698
  • 6
  • 112
  • 113
  • 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. – Torxed Feb 25 '19 at 09:29
  • Can you suggest me to store the database data in text file – Gowtham Muruvanda Feb 25 '19 at 09: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. – Reut Sharabani Feb 25 '19 at 09:33
  • I think you are looking for something like this https://stackoverflow.com/questions/21270148/fastest-way-to-write-database-table-to-file-in-python.. posted code will not work anyways – user_D_A__ Feb 25 '19 at 09: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. – Torxed Feb 25 '19 at 09:39
  • @GowthamMuruvanda I have posted an answer, kindly check. But still, as others suggest, you should try and solve these minor issues yourself rather than looking for help. Have fun coding – Devanshu Misra Feb 25 '19 at 09:41

1 Answers1

0

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.

Devanshu Misra
  • 773
  • 1
  • 9
  • 28
  • 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 "", line 5 file.close() ^ SyntaxError: invalid syntax – Gowtham Muruvanda Feb 25 '19 at 09:49
  • 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` – Devanshu Misra Feb 25 '19 at 10:33