Problem Solution Discussion Specifying the Datafile Location

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

10.3.1 Problem

Youre not sure how t o t ell LOAD DATA where t o look for your dat afile, part icularly if it s locat ed in anot her direct ory.

10.3.2 Solution

I t s a m at t er of knowing t he rules t hat det erm ine where MySQL looks for t he file.

10.3.3 Discussion

When you issue a LOAD DATA st at em ent , t he MySQL server norm ally assum es t he dat afile is locat ed on t he server host . However, you m ay not be able t o load dat a t hat way: • I f you access t he MySQL server from a rem ot e client host and have no m eans of t ransferring your file t o t he server host such as a login account t here , you wont be able t o put t he file on t he server. • Even if you have a login account on t he server host , your MySQL account m ust be enabled wit h t he FILE privilege, and t he file t o be loaded m ust be eit her world readable or locat ed in t he dat a direct ory for t he current dat abase. Most MySQL users do not hav e t he FILE privilege because it allows you t o do dangerous t hings , and you m ay not want t o m ake t he file world readable for securit y reasons or be able t o put it in t he dat abase direct ory. Fort unat ely, if you have MySQL 3.22.15 or lat er, you can load local files t hat are locat ed on t he client host by using LOAD DATA LOCAL rat her t han LOAD DATA . The only perm ission you need t o im port a local file is t he abilit y t o read t he file yourself. [ 2] [ 2] As of MySQL 3.23.49, use of t he LOCAL keyw ord m ay be disabled by default . You m ay be able t o t urn it on using t he - - local- infile opt ion for m ysql. I f t hat doesnt work, your server has been configured not t o allow LOAD DATA LOCAL at all. I f t he LOCAL keyword is not present , MySQL looks for t he dat afile on t he server host using t he following rules: • An absolut e pat hnam e fully specifies t he locat ion of t he file, beginning from t he root of t he filesyst em . MySQL reads t he file from t he given locat ion. • A relat ive pat hnam e is int erpret ed t wo ways, depending on whet her it has a single com ponent or m ult iple com ponent s. For a single-com ponent filenam e like m yt bl.t xt , MySQL looks for t he file in t he dat abase direct ory for t he current dat abase. For a m ult iple-com ponent filenam e like xyz m yt bl.t xt , MySQL looks for t he file beginning in t he MySQL dat a direct ory. I t expect s t o find m yt bl.t xt in a direct ory nam ed xyz. Dat abase direct ories are locat ed direct ly under t he dat a direct ory, so t hese t wo st at em ent s are equivalent if t he current dat abase is cookbook : mysql LOAD DATA INFILE mytbl.txt INTO TABLE mytbl; mysql LOAD DATA INFILE cookbookmytbl.txt INTO TABLE mytbl; I f t he LOCAL keyword is specified, MySQL looks for t he file on t he client host , and int erpret s t he pat hnam e t he sam e way your com m and int erpret er does: • An absolut e pat hnam e fully specifies t he locat ion of t he file, beginning from t he root of t he filesyst em . • A relat ive pat hnam e is int erpret ed relat ive t o your current direct ory. I f your file is locat ed on t he client host , but you forget t o indicat e t hat it s local, youll get an er r or . mysql LOAD DATA mytbl.txt INTO TABLE mytbl; ERROR 1045: Access denied for user: cbuserlocalhost Using password: YES That Access denied m essage can be confusing, given t hat if youre able t o connect t o t he server and issue t he LOAD DATA st at em ent , it would seem t hat youve already gained access t o MySQL. What t he error m essage m eans is t hat t he MySQL t ried t o open m yt bl.t xt on t he server host and could not access it . m ysqlim port uses t he sam e rules for finding files as LOAD DATA . By default , it assum es t he dat afile is locat ed on t he server host . To use a local file, specify t he - - local or - L opt ion on t he com m and line. LOAD DATA assum es t he t able is locat ed in t he current dat abase unless you specify t he dat abase nam e explicit ly. m ysqlim port always requires a dat abase argum ent : mysqlimport --local cookbook mytbl.txt I f you want t o use LOAD DATA t o load a file int o a dat abase ot her t han t he current one, you can qualify t he t able nam e wit h t he dat abase nam e. The following st at em ent does t his, indicat ing t hat t he mytbl t able is locat ed in t he other_db dat abase: mysql LOAD DATA LOCAL mytbl.txt INTO TABLE other_db.mytbl; LOAD DATA assum es no relat ionship bet ween t he nam e of t he dat afile and t he nam e of t he t able int o which youre loading t he files cont ent s. m ysqlim port assum es a fixed relat ionship bet ween t he dat afile nam e and t he t able nam e. Specifically, it uses t he last com ponent of t he filenam e t o det erm ine t he t able nam e. For exam ple, m ysqlim port would int erpret m yt bl.t xt , m yt bl.dat , t m p m yt bl.t xt , u paul dat a m yt bl.csv, and D: \ proj ect s\ m yt bl.t xt all as files cont aining dat a for t he mytbl t able. 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