Problem Solution Discussion Specifying the Datafile Format

Naming Datafiles Under Windows Windows syst em s use \ as t he pat hnam e separat or in filenam es. That s a bit of a problem , because MySQL int erpret s backslash as t he escape charact er in st ring values. To specify a Windows pat hnam e, eit her use doubled backslashes, or use forward slashes inst ead. These t wo st at em ent s show t wo ways of referring t o t he sam e Windows file: mysql LOAD DATA LOCAL INFILE D:\\projects\\mydata.txt INTO mytbl; mysql LOAD DATA LOCAL INFILE D:projectsmydata.txt INTO mytbl;

10.4 Specifying the Datafile Format

10.4.1 Problem

You have a dat afile t hat s not in LOAD DATA s default form at .

10.4.2 Solution

Use FIELDS and LINES clauses t o t ell LOAD DATA how t o int erpret t he file.

10.4.3 Discussion

By default , LOAD DATA assum es t hat dat afiles cont ain lines t hat are t erm inat ed by linefeeds newlines and t hat dat a values wit hin a line are separat ed by t abs. The following st at em ent does not specify anyt hing about t he form at of t he dat afile, so MySQL assum es t he default form at : mysql LOAD DATA LOCAL INFILE mytbl.txt INTO TABLE mytbl; To specify a file form at explicit ly, use a FIELDS clause t o describe t he charact erist ics of fields w it hin a line, and a LINES clause t o specify t he line-ending sequence. The following LOAD DATA st at em ent specifies t hat t he dat afile cont ains values separat ed by colons and lines t erm inat ed by carriage ret urns: mysql LOAD DATA LOCAL INFILE mytbl.txt INTO TABLE mytbl - FIELDS TERMINATED BY : - LINES TERMINATED BY \r; Each clause follows t he t able nam e. I f bot h are present , t he FIELDS clause m ust precede t he LINES clause. The line and field t erm inat ion indicat ors can cont ain m ult iple charact ers. For exam ple, \r\n indicat es t hat lines are t erm inat ed by carriage ret urn linefeed pairs. I f you use m ysqlim port , com m and- line opt ions provide t he form at specifiers. m ysqlim port com m ands t hat correspond t o t he preceding t wo LOAD DATA st at em ent s look like t his: mysqlimport --local cookbook mytbl.txt mysqlimport --local --fields-terminated-by=: --lines-terminated-by=\r \ cookbook mytbl.txt The order in which you specify t he opt ions doesnt m at t er for m ysqlim port , except t hat t hey should all precede t he dat abase nam e. Specifying Binary Format Option Characters As of MySQL 3.22.10, you can use hex not at ion t o specify arbit rary form at charact ers for FIELDS and LINES clauses. Suppose a dat afile has lines wit h Ct rl- A bet w een fields and Ct rl- B at t he end of lines. The ASCI I values for Ct rl- A and Ct rl- B are 1 and 2, so you represent t hem as 0x01 and 0x02 : FIELDS TERMINATED BY 0x01 LINES TERMINATED BY 0x02 m ysqlim port underst ands hex const ant s for form at specifiers as of MySQL 3.23.30. You m ay find t his capabilit y helpful if you dont like rem em bering how t o t ype escape sequences on t he com m and line or when it s necessary t o use quot es around t hem . Tab is 0x09 , linefeed is 0x0a , and carriage ret urn is 0x0d . Her es an exam ple t hat indicat es t hat t he dat afile cont ains t ab-delim it ed lines t erm inat ed by CRLF pairs: mysqlimport --local --lines-terminated-by=0x0d0a \ --fields-terminated-by=0x09 cookbook mytbl.txt

10.5 Dealing with Quotes and Special Characters