I am using MySQL to load a CSV file, but there is no quotes for the string column, so I only get the first letter of the value.
1,Toyota Park,Bridgeview,IL,0
2,Columbus Crew Stadium,Columbus,OH,0
3,RFK Stadium,Washington,DC,0
After I use the code here, I got this:
LOAD DATA LOCAL INFILE 'C:\\Users\\tank\\Desktop\\test.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 0 ROWS;
and I had this table:
+---------------------------------------------+
| ID, Studios, City, State, Open |
+---------------------------------------------+
| '1', 'T', 'B', 'I', '0' |
| '2', 'C', 'C', 'O', '0' |
| '3', 'R', 'W', 'D', '0' |
+---------------------------------------------+
You should use enclosed by:
LOAD DATA LOCAL INFILE 'C:\\Users\\tank\\Desktop\\test.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
ENCLOSED BY '\"'
ESCAPED BY '\"'
IGNORE 0 ROWS;
Related
I have a csv with 3 date columns but each is formatted dd/mm/yyyy H:i:s i.e 27/05/2019 20:25:00
I am trying to manipulate these to insert using LOAD DATA INFILE without any success:
My state looks like this:
LOAD DATA LOCAL INFILE '/file.csv'
INTO TABLE db_table FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
SET promotion_starts = str_to_date(#column7, '%d/%m/%Y %H:%i:%s'),
promotion_ends = str_to_date(#column8, '%d/%m/%Y %H:%i:%s'),
date_added = str_to_date(#column17, '%d/%m/%Y %H:%:%i:%s')
All other data inserts fine but the date columns are all null
You're missing the line of the query that specifies how the fields of the CSV file correspond to table columns, and defines #column7 and #column8.
LOAD DATA LOCAL INFILE '/file.csv'
INTO TABLE db_table FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(foo, bar, baz, xxx, yyy, zzz, #column7, #column8, aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, #column17, iii, jjj)
SET promotion_starts = str_to_date(#column7, '%d/%m/%Y %H:%i:%s'),
promotion_ends = str_to_date(#column8, '%d/%m/%Y %H:%i:%s'),
date_added = str_to_date(#column17, '%d/%m/%Y %H:%:%i:%s')
Replace all the column names I made up with the actual column names in your table that correspond to the CSV fields.
I have a table like this:
CREATE TABLE `tblinquiries` (
`UID` varchar(50) DEFAULT NULL,
`ReviewDate` date NOT NULL,
`InquiryId` varchar(50) DEFAULT NULL,
`AuditStatus` varchar(50) DEFAULT NULL,
PRIMARY KEY (`InquiryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have a csv file with data:
UID,ReviewDate,InquiryId,AuditStatus
UID1,2018-07-06,109814969,Check
UID2,2018-07-06,109866072,Check
UID3,2018-07-06,109911408,Check
UID4,2018-07-06,109798278,Check
I use below command to to upload the data:
$location = '../uploads/';
$name = $_FILES["file"]["name"];
$filePath = $location.$name;
$table = 'tblinquiries';
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
It uploads the data but adds extra character "\r" from the second line. I exported the data and got like below:
('UID4', '2018-07-06', '109798278', 'Check'),
('UID1', '2018-07-06', '109814969', 'Check\r'),
('UID2', '2018-07-06', '109866072', 'Check\r'),
('UID3', '2018-07-06', '109911408', 'Check\r');
After running:
SELECT AuditStatus, LENGTH(AuditStatus) FROM `tblinquiries`
got:
AuditStatus LENGTH(AuditStatus)
Check 5
Check 6
Check 6
Check 6
How can I solve this?
I would assume that your source data has those \r control characters, because LOAD DATA doesn't typically add data to the source file (unless you tell it to do so, which does not appear to be the case). We can try running RTRIM on the AuditStatus column:
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
(UID, ReviewDate, InquiryId, #AuditStatus)
SET AuditStatus = RTRIM(#AuditStatus);
As #Sloan suggested I changed the line terminators and that solved the problem.
Here is the final code.
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES
I'm trying to import a dataset that includes some text fields that are enclosed by double quotes "sometext" but when I try to load the file it still shows the double quotes.
Here is the sql
LOAD DATA LOCAL INFILE '/Users/Carlos/Desktop/smallData.txt'
into table acars
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#Date, #Time, flight, n_number, blk_id, msg_label, text_label, msg_text)
SET Date_Time = STR_TO_DATE(CONCAT(#Date, ' ', #Time), '%d/%m/%Y %H:%i');
with this small data sample:
date_dd_mm_yyyy; time_est; flight; n_number; blk_id; msg_label; text_label; msg_text
01/03/2014; 00:02; XA0001; ..VQBZZ; 3; 15; ""; " (2N26451W 81508---100354---(Z"
01/03/2014; 00:04; CO1588; .N71411; 7; H1; "msg to/from terminal"; " #DFB96 0 877"
01/03/2014; 00:12; XA0001; ..VQBZZ; 4; 15; ""; " (2N27038W 82483--- 94423---(Z"
the Problem is that I get this format back for all the lines:
| 2014-03-01 00:02:00 | XA0001 | ..VQBZZ | 3 | 15 | "" | " (2N26451W 81508---100354---(Z" |
basically it just doesn't do the OPTIONALLY ENCLOSED BY '"' part
I have a csv file which contains a lot of sql keywords. How do i escape the keywords?
sample row from the csv file:
F Aabha Hanjura 115 O/o District Fire Officer Chittor Dist . Chittoor AP 500032 500032 Chittoor AP 9963736976 Ap_Airlt_Pst ANDHRA PRADESH AIRTEL
This is what my import statement looks like:
LOAD DATA INFILE "/home/kannel/2.csv" INTO TABLE number_data COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n';
How can I import a TSV file when the numbers use comma as a decimal separator?
LOAD DATA INFILE '$filename' INTO TABLE dados_meteo IGNORE 3 LINES
($fields[0], $fields[1], $fields[2], $fields[3], $fields[4], $fields[5])
SET POM='$pom'
;
Try to replace ',' to '.' when loading.
For example -
LOAD DATA INFILE 'file.csv' INTO TABLE dados_meteo
(#var1, #var2)
SET column1 = REPLACE(#var1, ',', '.'), column2 = REPLACE(#var2, ',', '.')
If $field[0] is your numeric:
LOAD DATA INFILE '$filename' INTO TABLE dados_meteo IGNORE 3 LINES
(#var1, $fields[1], $fields[2], $fields[3], $fields[4], $fields[5])
SET POM='$pom', $field[0] = CONVERT(REPLACE(#var1,',', ''), DECIMAL(10));
If more are numeric, simply repeat the pattern with a #var2, #var3, etc. You'll want to replace the DECIMAL(10) with whatever your field really is.