Import csv files into MySQL table
Posted: Mon Apr 25, 2016 2:47 pm
My 2016 Volvo XC60 T6 comes with Volvo On Call that is handy. It keeps driving journal that helps organize and analyze the driving performance.
The report comes with the following fields:
Category;Started;Start odometer (miles);Start address;Stopped;End odometer (miles);End address;Duration;Distance (miles);Fuel consumption (gallons);Title;User Notes;
I manually created a table to accommodate these fields.
Some steps need to be taken before the import will work.
1. Use 'notepad' CTRL+H to replace " h" to "" on the csv file.
2. Change "duration" format from "HH:MM h" to "HH:MM:SS".
3. Change "started" and "stopped" datetime format from "MM/DD/YY HH:MM APM" to "YYYY-MM-DD HH:MM:SS"
4. Remove the last 2 empty columns (Title and User Notes)
5. Sort "started" with ascending (from earliest to latest)
6. Replace Category "Unassigned" with the index key
7. Remove the first line/row of the file
8. Upload the csv file to the server (hsiao.net)
9. Use "dos2unix" to convert the file
10. Import with the following sql command in workbench (Execute SQL Script in Connected Server) or command line:
It will report some the following messages:
The report comes with the following fields:
Category;Started;Start odometer (miles);Start address;Stopped;End odometer (miles);End address;Duration;Distance (miles);Fuel consumption (gallons);Title;User Notes;
I manually created a table to accommodate these fields.
Some steps need to be taken before the import will work.
1. Use 'notepad' CTRL+H to replace " h" to "" on the csv file.
2. Change "duration" format from "HH:MM h" to "HH:MM:SS".
3. Change "started" and "stopped" datetime format from "MM/DD/YY HH:MM APM" to "YYYY-MM-DD HH:MM:SS"
4. Remove the last 2 empty columns (Title and User Notes)
5. Sort "started" with ascending (from earliest to latest)
6. Replace Category "Unassigned" with the index key
7. Remove the first line/row of the file
8. Upload the csv file to the server (hsiao.net)
9. Use "dos2unix" to convert the file
Code: Select all
# dos2unix <original csv file> <new csv file>
Code: Select all
LOAD DATA INFILE '/<PATH>/<new csv file>'
INTO TABLE Automobile.2016_Volvo_XC60_VOC
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Code: Select all
199 row(s) affected
Records: 199 Deleted: 0 Skipped: 0 Warnings: 0