Problem Solution Discussion Importing Data with LOAD DATA and mysqlimport

Som e shell com m ands are so long t hat t heyre shown as you would ent er t hem using several lines, wit h a backslash charact er as t he line-cont inuat ion charact er: prog_name \ argument1 \ argument2 ... That works for Unix, but not for Windows, where youll need t o om it t he cont inuat ion charact ers and t ype t he ent ire com m and on one line: C:\ prog_name argument1 argument2 ...

10.2 Importing Data with LOAD DATA and mysqlimport

10.2.1 Problem

You want t o load a dat afile int o a t able using MySQLs built in im port capabilit ies.

10.2.2 Solution

Use t he LOAD DATA st at em ent or t he m ysqlim port com m and- line program .

10.2.3 Discussion

MySQL provides a LOAD DATA st at em ent t hat act s as a bulk dat a loader. Heres an exam ple st at em ent t hat reads a file m yt bl.t xt from your current direct ory and loads it int o t he t able mytbl in t he current dat abase: mysql LOAD DATA LOCAL INFILE mytbl.txt INTO TABLE mytbl; MySQL also includes a ut ilit y program nam ed m ysqlim port t hat act s as a wrapper around LOAD DATA so t hat you can load input files direct ly from t he com m and line. The m ysqlim port com m and t hat is equivalent t o t he preceding LOAD DATA st at em ent looks like t his, assum ing t hat mytbl is in t he cookbook dat abase: [ 1] [ 1] For m ysqlim port , as wit h ot her MySQL program s, you m ay need t o specify connect ion param et er opt ions such as - - user or - - host . I f so, t hey should precede t he dat abase nam e argum ent . mysqlimport --local cookbook mytbl.txt The following list describes LOAD DATA s general charact erist ics and capabilit ies; m ysqlim port shares m ost of t hese behaviors. There are som e differences t hat well not e as we go along, but for t he m ost part you can read LOAD DATA as LOAD DATA or m ysqlim port . LOAD DATA provides opt ions t o address m any of t he im port issues m ent ioned in t he chapt er int roduct ion, such as t he line-ending sequence for recognizing how t o break input int o records, t he colum n value delim it er t hat allows records t o be broken int o separat e values, t he quot ing charact er t hat m ay surround colum n values, quot ing and escaping issues wit hin values, and NULL value represent at ion: • By default , LOAD DATA expect s t he dat afile t o cont ain t he sam e num ber of colum ns as t he t able int o which youre loading dat a, and t he dat afile colum ns m ust be present in t he sam e order as in t he t able. I f t he file doesnt cont ain a value for every colum n or t he values arent in t he proper order, you can specify which colum ns are present and t he order in which t hey appear. I f t he dat afile cont ains fewer colum ns t han t he t able, MySQL assigns default values t o colum ns for which no values are present in t he dat afile. • LOAD DATA assum es t hat dat a values are separat ed by t ab charact ers and t hat lines end wit h linefeeds newlines . You can specify t he dat a form at explicit ly if a file doesnt conform t o t hese convent ions. • You can indicat e t hat dat a values m ay have quot es around t hem t hat should be st ripped, and you can specify what t he quot e charact er is. • Several special escape sequences are recognized and convert ed during input processing. The default escape charact er is backslash \ , but you can change it if you like. The \N sequence is t aken t o represent a NULL value. The \b , \n , \r , \t , \\ , and \0 sequences are int erpret ed as backspace, linefeed, carriage ret urn, t ab, backslash, and ASCI I NUL charact ers. NUL is a zero- valued byt e, which is different t han t he SQL NULL value. • LOAD DATA provides diagnost ic inform at ion, but it s a sum m ary t hat doesnt give you specific inform at ion about which input lines m ay have caused problem s. There is work in progress for MySQL 4 on providing im proved feedback. I n t he m eant im e, see Recipe 10.38 , w hich describes a LOAD DATA diagnost ic ut ilit y. The next few sect ions describe how t o im port dat afiles int o MySQL t ables using LOAD DATA or m ysqlim port . They assum e your files cont ain legal dat a values t hat are accept able t o MySQL. Why m ake t his assum pt ion? Because alt hough LOAD DATA has several opt ions t hat cont rol how it reads t he dat afile, t heyre concerned only wit h t he st ruct ure of t he file. LOAD DATA wont validat e or reform at dat a values for you. I t s necessary t o perform such operat ions eit her by preprocessing t he dat afile before loading it , or by issuing SQL st at em ent s aft er loading it . I f you need t o check or reform at an input file first t o m ake sure it s legal, several sect ions lat er in t he chapt er show how t o do t hat .

10.3 Specifying the Datafile Location