0

I have multiple unstructured txt files in a directory and I want to insert all of them into mysql; basically, the entire content of each text file should be placed into a row . In MySQL, I have 2 columns: ID (auto increment), and LastName(nvarchar(45)). I used Python to connect to MySql; used LOAD DATA LOCAL INFILE to insert the whole content. But when I run the code I see the following messages in Python console: enter image description here. Also, when I check MySql, I see nothing but a bunch of empty rows with Ids being automatically generated. enter image description here Here is the code:

import MySQLdb
import sys
import os
result = os.listdir("C:\\Users\\msalimi\\Google Drive\\s\\Discharge_Summary")
for x in result:
     db = MySQLdb.connect("localhost", "root", "Pass", "myblog")
     cursor = db.cursor()
     file1 = os.path.join(r'C:\\Discharge_Summary\\'+x)     
     cursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE clamp_test" %(file1,));         
     db.commit()
db.close()

Can someone please tell me what is wrong with the code? What is the right way to achieve my goal?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
nina_dev
  • 625
  • 2
  • 10
  • 19

1 Answers1

0

I edited my code with:

.....cursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE clamp_test LINES TERMINATED BY '\r' (Lastname) SET id = NULL" %(file1,)) 

and it worked :)

nina_dev
  • 625
  • 2
  • 10
  • 19