Epilog Importing and Exporting Data

exit 0; The script creat es an XML::XPath obj ect , which opens and parses t he docum ent . Then t he obj ect is queried for t he set of row elem ent s, using t he pat h row . The size of t his set indicat es how m any records t he docum ent cont ains. To process each row, t he script uses t he pat h t o ask for all t he children of t he row obj ect . Each child corresponds t o a colum n wit hin t he row; using as t he pat h for get_nodelist t his way is convenient because we need not know in advance which colum ns t o expect . xm l_t o_m ysql.pl obt ains t he nam e and value from each colum n and saves t hem in t he name and value arrays. Aft er all t he colum ns have been processed, t he arrays are used t o const ruct an INSERT st at em ent t hat nam es t hose colum ns t hat were found t o be present in t he row and t hat includes a placeholder for each dat a value. Recipe 2.7 discusses placeholder list const ruct ion. Then t he script issues t he st at em ent , passing t he colum n values t o do t o bind t hem t o t he placeholders. I n t he previous sect ion, we used m ysql_t o_xm l.pl t o export t he cont ent s of t he expt t able as an XML docum ent . xm l_t o_m ysql.pl can be used t o perform t he converse operat ion of im port ing t he docum ent back int o MySQL: xml_to_mysql.pl cookbook expt expt.xml As it processes t he docum ent , t he script generat es and execut es t he following set of st at em ent s: INSERT INTO expt subject,test,score VALUES Jane,A,47 INSERT INTO expt subject,test,score VALUES Jane,B,50 INSERT INTO expt subject,test VALUES Jane,C INSERT INTO expt subject,test VALUES Jane,D INSERT INTO expt subject,test,score VALUES Marvin,A,52 INSERT INTO expt subject,test,score VALUES Marvin,B,45 INSERT INTO expt subject,test,score VALUES Marvin,C,53 INSERT INTO expt subject,test VALUES Marvin,D Not e t hat t hese st at em ent s do not all insert t he sam e num ber of colum ns. St at em ent s wit h m issing colum ns correspond t o rows wit h NULL values.

10.44 Epilog

Recall t he scenario wit h which t his chapt er began: Suppose you have a file nam ed som edat a.csv t hat cont ains 12 colum ns of dat a in com m a- separat ed values CSV form at . From t his file you want t o ext ract only colum ns 2, 11, 5, and 9, and use t hem t o creat e dat abase records in a MySQL t able t hat cont ains name , birth , height , and weight colum ns. You need t o m ake sure t hat t he height and w eight are posit ive int egers, and convert t he birt h dat es from MMDDYY form at t o CCYY-MM-DD form at . How can you do t his? So ... how would you do t hat , based on t he t echniques discussed in t his chapt er? Much of t he work can be done using t he ut ilit y program s developed here. You can convert t he file t o t ab-delim it ed form at wit h cvt _file.pl, ext ract t he colum ns in t he desired order wit h yank_col.pl, and rewrit e t he dat e colum n t o I SO form at wit h cvt _dat e.pl: cvt_file.pl --iformat=csv somedata.csv \ | yank_col.pl --columns=2,11,5,9 \ | cvt_date.pl --columns=2 --iformat=us --add-century tmp The result ing file, t m p, will have four colum ns represent ing t he name , birth , height , and weight values, in t hat order. I t needs only t o have it s height and weight colum ns checked t o m ake sure t hey cont ain posit ive int egers. Using t he is_positive_integer library funct ion from t he Cookbook_Ut ils.pm m odule file, t hat t ask can be achieved using a short special-purpose script t hat isnt m uch m ore t han an input loop: usrbinperl -w validate_htwt.pl - heightweight validation example Assumes tab-delimited, linefeed-terminated input lines. Input columns and the actions to perform on them are as follows: 1: name; echo as given 2: birth; echo as given 3: height; validate as positive integer 4: weight; validate as positive integer use strict; use lib qwusrlocalapachelibperl; use Cookbook_Utils; while { chomp; my name, birth, height, weight = split \t, _, 4; warn line .:height height is not a positive integer\n if is_positive_integer height; warn line .:weight weight is not a positive integer\n if is_positive_integer weight; } exit 0; The validat e_ht wt .pl script doesnt produce any out put except for warning m essages , because it doesnt need t o reform at any of t he input values. Assum ing t hat t m p passes validat ion wit h no errors, it can be loaded int o MySQL wit h a sim ple LOAD DATA st at em ent : mysql LOAD DATA LOCAL INFILE tmp INTO TABLE tbl_name ;

Chapter 11. Generating and Using Sequences

I nt roduct ion Using AUTO_I NCREMENT To Set Up a Sequence Colum n Generat ing Sequence Values Choosing t he Type for a Sequence Colum n The Effect of Record Delet ions on Sequence Generat ion Ret rieving Sequence Values Det erm ining Whet her t o Resequence a Colum n Ext ending t he Range of a Sequence Colum n Renum bering an Exist ing Sequence Reusing Values at t he Top of a Sequence Ensuring That Rows Are Renum bered in a Part icular Order St art ing a Sequence at a Part icular Value Sequencing an Unsequenced Table Using an AUTO_I NCREMENT Colum n t o Creat e Mult iple Sequences Managing Mult iple Sim ult aneousAUTO_I NCREMENT Values Using AUTO_I NCREMENT Valuest o Relat e Tables Using Single- Row Sequence Generat ors Generat ing Repeat ing Sequences Num bering Query Out put Rows Sequent ially