1

I try to import data into a mySQL database using Python, but I can't get it to work. I don't get any errors it looks as if everything is working OK, but the text file never gets imported.

I can import the text file just fine if I do it manually via the mysql command line in Terminal. What am I doing wrong?

imoprt mysql.connector
cnx = mysql.connector.connect(user ='user1', password ='12345', host ='127.0.0.1', database ='stockStatus')
cursor = cnx.cursor()
cursor.execute('use stockStatus')
cursor.execute('truncate table products')
cursor.execute("LOAD DATA INFILE '/Path/products.txt' INTO TABLE products IGNORE 1 LINES")
cnx.close()
4
  • take a look here stackoverflow.com/questions/25573009/… Commented Sep 30, 2014 at 15:55
  • you can also switch to mysql connector python v2.0.1 which has LOCAL INFILE enable by default Commented Sep 30, 2014 at 15:56
  • I'm already on v2.0.1 Commented Sep 30, 2014 at 18:58
  • Ok, I think the problem is you are not doing cnx.commit() at the end. By default autocommit is set to False, there fore you have to explicitly call commit, or make the connection with autocommit=True Commented Oct 1, 2014 at 7:22

1 Answer 1

0

Try using double quotes around the input file name:

cursor.execute('LOAD DATA INFILE "/Path/products.txt" INTO TABLE products IGNORE 1 LINES')
Sign up to request clarification or add additional context in comments.

1 Comment

No, that didn't do it.

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.