问题
I am trying to import a CSV file from to a mysql database from the command line. This will be later incorporated into a Windows batch file.
mysqlimport -u user -puserpw --columns=ID,CID,Alerted --fields-terminated-by=',' --local School Customer.csv
All the data loads into the first column in the Customer table. I want to correctly import data from the CSV to the appropriate column.
CSV Data format:
ID,CID,Alerted
1,CS,N
2,CS,N
3,CS,N
I would like to use mysqlimport since this will be easier to add to a Windows batch file. How can I do this please help?
回答1:
I guess you have to add --lines-terminated-by='\n' with mysqlimport. I run a quick test here and worked.
mysqlimport --local --ignore-lines=1 --fields-terminated-by=',' --lines-terminated-by='\n' db_name table_name.csv
回答2:
I had to enclose the values in double quotes "
mysqlimport -u user -puserpw --columns=ID,CID,Alerted --ignore-lines=1 --fields-terminated-by="," --lines-terminated-by="\n" --local School Customer.csv
That fixed it.
回答3:
Try to use the complete description for importing the csv file like :
fields terminated by ',' enclosed by '"' lines terminated by '\n'
Thanks
来源:https://stackoverflow.com/questions/23002443/mysqlimport-import-csv-file-in-ms-windows-xampp-emvironment