• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mysql database - reading newline characters

 
Puspender Tanwar
Ranch Hand
Posts: 397
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
was reading article on mysql and found some code and lines there
If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:


for what '\r\n' is used for ??
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are special characters to represent the codes for "carriage return" and "line feed" which indicate a new line: https://en.m.wikipedia.org/wiki/Newline

In other words, they tell the computer that you want to move the reading position to the start of the next line. If you've seen or used an old typewriter, you will know that these are separate actions, i.e. return slides the carriage horizontally so the typing position is at the start of the current line, and line-feed moves the paper up a bit so you are typing on an empty line. Of course, the typewriter also allows you to combine these in a single action in normal use.

Different operating systems represent these special newline indicators in different ways, so the data-load code you found is telling MySQL how to recognise the new line as \r\n in a text file on Windows. Unix combines them both as \n, a bit like the combined action on the typewriter.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've put this post under General Computing, as it's not really a specific MySQL question.
 
Puspender Tanwar
Ranch Hand
Posts: 397
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means, \r\n is used for pointing the cursor to the starting of new line ?
don't we do this using \n in windows ?
and what this whole statement means LINES TERMINATED BY '\r\n';
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:means, \r\n is used for pointing the cursor to the starting of new line ?

Essentially, yes. But in this case you do not have a cursor on a screen, because you are reading a stream of characters from a file. In this example, the text within the file is presumably structured as a series of lines, so you need to know when the current "line" of incoming characters ends and the new one begins. If the file was in a binary format e.g. an image, it would not contain text "lines" at all.
Puspender Tanwar wrote:don't we do this using \n in windows ?

I don't do much of this kind of thing on Windows, but it probably depends on how the text file was created e.g. if the file came from Unix originally then it may only have "\n" or it may have been converted to use "\r\n" for Windows. When you move files between Windows and Unix, this is one of the things that can cause problems. Also, different tools may be able to handle this for you automatically if they know which OS they are running on. In this case it looks like you need to tell MySQL explicitly how to recognise the end of a "line" in the incoming stream of data.
Puspender Tanwar wrote:and what this whole statement means LINES TERMINATED BY '\r\n';


This means exactly what it says:
Load some data...
.. from a local input file called '/path/pet.txt'...
... into a table called 'pet'...
... and each incoming line ends with '\r\n'.

This file is probably in CSV format, which is commonly used for tabular data in flat files, although it's not explicitly stated here.

The MySQL documentation should help you with the details here.
 
Puspender Tanwar
Ranch Hand
Posts: 397
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you chris
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic