简体   繁体   中英

LOAD DATA LOCAL INFILE with incremental field

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: 在此处输入图片说明 . Also, when I check MySql, I see nothing but a bunch of empty rows with Ids being automatically generated. 在此处输入图片说明 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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM