MySQL Load Data enclosed character and field terminator on last field - mysql

I'm having a seemingly basic problem with loading data to MySQL. I'm using:
LOAD DATA LOCAL INFILE 'C:/.../Contrato.txt'
INTO TABLE schema.contrato
FIELDS TERMINATED BY ';' ENCLOSED BY '|' LINES TERMINATED BY '/r/n' IGNORE 0 LINES;
Each line on the file looks like this, generated by another program:
|abc|;|cde|;|123|;|456|;|name|\r\n
When executing the load, everything seems to load properly, except the very last field. When I look at the table, the last field actually shows the '|' characters around the name. It's not the end of the world, but it's strange that it would do that. As of now, I'm fixing it by adding a ';' right before the \r\n characters.
Is this the way it's supposed to be done?? Why would I need to add the field terminator before the line terminator in order to delete the field enclosers?

I can duplicate the effect on a file with a single line in it, with multiple lines I only get a single entry which has the final column entry of "name| |abc" .
I changed '/r/n' to '\r\n' & the load worked correctly for files with a single entry & multiple entries & the surrounding | were correctly removed
LOAD DATA LOCAL INFILE 'C:/.../Contrato.txt'
INTO TABLE schema.contrato
FIELDS TERMINATED BY ';' ENCLOSED BY '|' LINES TERMINATED BY '\r\n' IGNORE 0 LINES;
The correct MySQL escape character is backslash not forwardslash which is not treated as a special character - so your original code was looking for the 4 character sequence "forwardslash r forwardslash n" to terminate the lines

Related

MySQL: Importing csv into table with quotation marks

I have a csv where each row starts with quotation marks and ends with them too. How do I ignore the quotations at the start and end of the row when loading the csv into a table?
LOAD DATA LOCAL INFILE '/path/data.csv'
INTO TABLE test1
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\"\n'
IGNORE 1 ROWS;
I have tried
OPTIONALLY ENCLOSED BY '"'
but this refers to each specific field and not the entire row.
As commented by Shadow and Barmar, the answer lies in the documentation :
If all the input lines have a common prefix that you want to ignore, you can use LINES STARTING BY 'prefix_string' to skip the prefix and anything before it. If a line does not include the prefix, the entire line is skipped. [...] The FIELDS TERMINATED BY, LINES STARTING BY, and LINES TERMINATED BY values can be more than one character.
Hence, use :
LOAD DATA LOCAL INFILE '/path/data.csv'
INTO TABLE test1
FIELDS TERMINATED BY ';'
LINES STARTING BY '"'
LINES TERMINATED BY '"\n'
IGNORE 1 ROWS;

Unable to import UTF-8 .txt file into MySQLWorkbench

I have a .txt file in UTF-8 format with information I am trying to import in an already created table that has rows already in it.
The information in the .txt file is structured like this: (the quotes are included in the .txt file)
"Bob,Smith,25,California,,,,Single,"
"John,Doe,72,Nevada,,2,1,Married,"
"Will,Smith,22,Texas,1000005,2,1,Married,"
The query I'm using is:
LOAD DATA LOCAL INFILE 'myfile.txt' INTO TABLE mytable FIELDS ENCLOSED BY '"' TERMINATED BY ',' LINES TERMINATED BY '\n'
What happens is that all of these records get inserted but get inserted like this
Bob,null,null,null,null,null,null,null
John,null,null,null,null,null,null,null
Will,null,null,null,null,null,null,null
It's like the " is not being caught at the end or something weird . Am I doing something wrong here?
According to the example data provided your fields are not enclosed by quotes but rather the whole record is.
You can use the STARTING BY option to ignore the initial quote and the trailing one should be ignore automatically.
I think what you need is this:
LOAD DATA LOCAL INFILE 'your_file.txt' INTO TABLE your_table FIELDS TERMINATED BY ',' LINES STARTING BY '"' TERMINATED BY '\n';
The format of your text file does not make any sense. (Strangely enough, the way the importer handles it does not make any sense either. But we have no control over the importer, so let us ignore that.)
The double quotes must surround each field, not each line. The ENCLOSED BY '"' clause refers to the fields, not to the lines. So, you are telling the importer that your fields are enclosed in quotes, but you are enclosing your lines in quotes. So, the importer considers each one of your lines as a field.
(Then, the importer proceeds to further chop up your lines at the comma, which makes no sense because the commas are within the quotes, so it should be ignoring them, so the importer is kind of brain-damaged too.)
By using the FIELDS ENCLOSED BY '"' statement the input file should enclose EACH field data by a " therefor the input file should be as follows
"Bob","Smith","25","California","","","","Single",
"John","Doe","72","Nevada","","2","1","Married",
"Will","Smith","22","Texas","1000005","2","1","Married",
That should add the data into the fields

Importing a CSV in MySQL. Only every second line is imported

i want to import a csv table into mysql with this command:
LOAD DATA LOCAL INFILE 'C:/Users/user/Desktop/SA01505.csv' INTO TABLE hks.orc CHARACTER SET 'utf8' COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#var1,#var2,#var3,`TI 41-40`,`TI 41-41`,`TI 41-42`,`TI42-40`,`TI 42-41`,`TI 42-42`,`TI 42-43`,`TI 42-43.2`,`TI 42-44`,`TI 42-45`,`TI 42-46`,`TI 42-47`,`PI 42-71`,`PI 42-72`,`PI 42-73`,`PI 42-75`,`FI 42-90`,`TI 43-40`,`TI 43-41`,`TI 45-40`,`TI 45-41`,`TI 47-41`,`TI 47-42`,`TI 47-43`,`TI 47-44`,`LI 42-61`,`SI 44-81`,`QI 46-22`,`UV 41-10_Soll`,`UV 41-10_Ist`,`UV43-10_Soll`,`UV43-10_Ist`,`Tc2`,`Tc3`,`Tc4`)
SET Datum = STR_TO_DATE(CONCAT(#var2,#var3),'%Y/%m/%d%k:%i:%s');
This code works, but imports only every second line, although every line is terminated with "\n" (i checked this with an hex-editor)
I'd guess that the problem is here: OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'. This is biting each other.
In the first line imported the last " of the enclosed column escapes the \n of the line terminator. Therefore always two lines are treated as one and you probably don't see the "second line" in the imported data because the data gets truncated.
Remove the ESCAPED BY '"' clause and see if it works. If it works but you need it to properly import your data, you'll have to work on your data.

Exporting data escaping new lines not working

I need to export data from a mysql table to a csv file. Two fields in particular are giving me trouble. Their data types are varchar and TEXT.
Data in these columns contain all sorts of gross characters, particularly new line (\n) and tabs (\t). I need to export these into a .csv file for a migration.
Unfortunately, I cannot enclose the fields with '"' because the destination database doesn't support that formatting.
So, my query looks like the following:
SELECT `varchar_col`,`text_col` FROM `db`.`tbl` INTO OUTFILE '/path/to/my/file.csv' FIELDS TERMINATED BY '\t' ESCAPED BY "\\" LINES TERMINATED BY '\n';
When I look at my output file (used gedit and nano), I simply see that each new line or tab in the file is preceded by a backslash (see example below). I would like a new line or tab instead to read the literal chacters '\n' or '\t' instead of actual new lines and tabs.
Example of problem with new line-
field value of:
value one
value two
exported to .csv gives me:
value one\
value two\
instead of:
value 1\nvalue 2
Can anyone tell me what im doing wrong?
Thanks
I'm not quite sure. But is this what you want? (Output literal chacters '\n' or '\t' instead of actual new lines and tabs.)
SELECT * FROM block INTO OUTFILE '/var/tmp/d.csv' FIELDS TERMINATED BY '\\t' ESCAPED BY "\\" LINES TERMINATED BY '\\n';

Importing space-delimited data into MySQL

I have file in the following format:
`e00` `e01` `e02` `e03`
`e10` `e11` `e12` `e13
Trying to import the data with
LOAD DATA INFILE 'file' INTO TABLE 'foo' FIELDS TERMINATED BY ' ' ENCLOSED BY '`'
only seems to get the first 3 fields of each line. Is there a way to load the data without altering the file format?
Let's all jump in the way-back machine to answer a 5-year old question!
The fact that the last item is not being loaded is a big hint. According to the manual:
If LINES TERMINATED BY is an empty string and FIELDS TERMINATED BY is
nonempty, lines are also terminated with FIELDS TERMINATED BY.
So, it's looking for a trailing space at the end of the line.
You could add a space to the end of each line in your input file, or try LINES TERMINATED BY '\n'