Problem Solution Discussion Dealing with Quotes and Special Characters

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

10.5.1 Problem

Your dat afile cont ains quot ed values or escaped charact ers.

10.5.2 Solution

Tell LOAD DATA t o be aware of t hem so t hat it doesnt load t he values int o t he dat abase unint erpret ed.

10.5.3 Discussion

The FIELDS clause can specify ot her form at opt ions besides TERMINATED BY . By default , LOAD DATA assum es t hat values are unquot ed, and int erpret s t he backslash \ as an escape charact er for special charact ers. To indicat e t he value quot ing charact er explicit ly, use ENCLOSED BY ; MySQL will st rip t hat charact er from t he ends of dat a values during input processing. To change t he default escape charact er, use ESCAPED BY . The t hree subclauses of t he FIELDS clause ENCLOSED BY , ESCAPED BY , and TERMINATED BY m ay be present in any order if you specify m ore t han one of t hem . For exam ple, t hese FIELDS clauses are equivalent : FIELDS TERMINATED BY , ENCLOSED BY FIELDS ENCLOSED BY TERMINATED BY , The TERMINATED BY sequence can consist of m ult iple charact ers. I f dat a values are separat ed w it hin input lines by som et hing like , youd indicat e t hat like t his: FIELDS TERMINATED BY To disable escape processing ent irely, specify an em pt y escape sequence: FIELDS ESCAPED BY When you specify ENCLOSED BY t o indicat e t hat quot e charact ers should be st ripped from dat a values, it s possible t o include t he quot e charact er lit erally wit hin dat a values by doubling it or by preceding it wit h t he escape charact er. For exam ple, if t he quot e and escape charact ers are and \ , t he input value ab\c will be int erpret ed as abc . For m ysqlim port , t he corresponding com m and- line opt ions for specifying quot e and escape values ar e - - fields-enclosed-by and - - fields- escaped-by. When using m ysqlim port opt ions t hat include quot es or backslashes or ot her charact ers t hat are special t o your com m and int erpret er, rem em ber t hat you m ay need t o quot e or escape t he quot e or escape charact ers

10.6 Importing CSV Files