1

I am trying to add all words of a text file into a column such that one row has one word. my code is as :

 import MySQLdb
 conn = MySQLdb.connect (host = "localhost",user = "root", db = "pcorpora")
 c = conn.cursor()
 file = open('C:\Users\Admin\Desktop\english.txt', 'r')
 words = list(file.read())
 i=0
 for value in words:
    c.execute("""INSERT INTO tenglish (`english words`) VALUES (%s)""" % (words[i]) i=i+1)`                                                                                           

The code run without error but table is still empty.

1 Answer 1

1

You should use commit

c.execute("""INSERT INTO tenglish (`english words`) VALUES (%s)""" % (value))
con.commit()

This method sends a COMMIT statement to the MySQL server, committing the current transaction. Since by default Connector/Python does not autocommit, it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.

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

3 Comments

I again get error it says: Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> c.execute('query') File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'query' at line 1")
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'query' at line 1"
What does i=i+1 mean?

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.