Problem Solution Discussion Exporting Tables as Raw Data

Not e t hat NULL values are writ t en as t he st ring NULL . Som e sort of post processing m ay be necessary t o convert t hem , depending on what you want t o do wit h t he out put file. I t s possible t o produce out put in form at s ot her t han t ab-delim it ed by sending t he query result int o a post -processing filt er t hat convert s t abs t o som et hing else. For exam ple, t o use hash m arks as delim it ers, convert all t abs t o charact ers TAB indicat es w here you t ype a t ab charact er in t he com m and : mysql -N -e your query here db_name | sed -e s TAB g output_file You can also use t r for t his purpose, t hough t he synt ax m ay vary for different im plem ent at ions of t his ut ilit y. The com m and looks like t his for Mac OS X or RedHat Linux: mysql -N -e your query here db_name | tr \t output_file The m ysql com m ands j ust shown use - N or - ss t o suppress colum n labels from appearing in t he out put . Under som e circum st ances, it m ay be useful t o include t he labels. For exam ple, t hey m ay be useful when im port ing t he file lat er. I f so, om it t he label-suppression opt ion from t he com m and. I n t his respect , export ing query result s wit h m ysql is m ore flexible t han SELECT ... INTO OUTFILE because t he lat t er cannot produce out put t hat includes colum n labels.

10.14.6 See Also

Anot her way t o export query result s t o a file on t he client host is by using t he m ysql_t o_t ext .pl ut ilit y described in Recipe 10.18 . That program has opt ions t hat allow you t o specify t he out put form at explicit ly. To export a query result as an Excel spreadsheet or for use w it h FileMaker Pro, see Recipes Recipe 10.40 and Recipe 10.41 .

10.15 Exporting Tables as Raw Data

10.15.1 Problem

You want t o export an ent ire t able t o a file.

10.15.2 Solution

Use t he m ysqldum p program wit h t he - - t ab opt ion.

10.15.3 Discussion

The m ysqldum p program is used t o copy or back up t ables and dat abases. I t can writ e t able out put eit her as a raw dat afile, or as a set of INSERT st at em ent s t hat recreat e t he records in t he t able. The form er capabilit y is described here, t he lat t er in Recipe 10.16 and Recipe 10.17 . To dum p a t able as a dat afile, you m ust specify a - - t ab opt ion t hat indicat es t he direct ory where you want t he MySQL server t o writ e t he file. The direct ory m ust already exist ; t he server wont creat e it . For exam ple, t o dum p t he states t able from t he cookbook dat abase t o a file in t he t m p direct ory, use a com m and like t his: mysqldump --no-create-info --tab=tmp cookbook states m ysqldum p creat es a dat afile using t he t able nam e plus a .t xt suffix, so t his com m and will writ e a file nam ed t m p st at es.t xt . This form of m ysqldum p is in som e respect s t he com m and- line equivalent of SELECT ... INTO OUTFILE . For exam ple, it writ es out a t able as a dat afile on t he server host , and you m ust have t he FILE privilege t o use it . See Recipe 10.14 for a list of general propert ies of SELECT ... INTO OUTFILE . I f you om it t he - - no- creat e- info opt ion, m ysqldum p also will creat e a file t m p st at es.sql t hat cont ains t he CREATE TABLE st at em ent for t he t able. The lat t er file will be owned by you, unlike t he dat afile, which is owned by t he server. You can nam e m ult iple t ables aft er t he dat abase nam e, in which case m ysqldum p writ es out put files for each of t hem . I f you dont nam e any t ables, m ysqldum p writ es out put for every t able in t he dat abase. m ysqldum p creat es dat afiles in t ab-delim it ed, linefeed- t erm inat ed form at by default . To cont rol t he out put form at , use t he - - fields-enclosed-by, - - fields- t erm inat ed-by, and - - lines- t erm inat ed-by opt ions t hat is, t he sam e opt ions t hat m ysqlim port underst ands as form at specifiers . For exam ple, t o writ e t he states t able in CSV form at wit h CRLF line endings, use t his com m and: mysqldump --no-create-info --tab=tmp \ --fields-enclosed-by=\ --fields-terminated-by=, \ --lines-terminated-by=\r\n cookbook states A dat afile export ed t his way can be im port ed using LOAD DATA or m ysqlim port . Be sure t o use m at ching form at specifiers when im port ing if you didnt dum p t he t able using t he default form at .

10.16 Exporting Table Contents or Definitions in SQL Format