11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 38 • IMPORTING AND EXPORTING DATA• [(SET column_name = expression,...)]: If the number of fields located in thetarget file does not match the number of fields in the target table, you needto specify exactly which columns are to be filled in by the file data. Forexample, if the target file containing sales information consists of only fourfields (id, client_id, order_time, and total_cost) rather than the six fieldsused in prior examples (id, client_id, order_time, sub_total, shipping_cost,and total_cost), yet in the target table all six fields remain, the commandwould have to be written like so:LOAD DATA INFILE "sales.txt"INTO TABLE sales (id, client_id, order_time, total_cost);Keep in mind that such attempts could fail should one or several of themissing columns be designated as NOT NULL in the table schema. On suchoccasions, you need to either designate DEFAULT values for the missingcolumns or further manipulate the data file into an acceptable format.You can also set columns to variables such as the current timestamp. Forexample, presume the sales table was modified to include an additionalcolumn named added_to_table:LOAD DATA INFILE "sales.txt"INTO TABLE sales (id, client_id, order_time, total_cost)SET added_to_table = CURRENT_TIMESTAMP;■ Tip If you would like the order of the fields located in the target file to be rearranged as they are read infor insertion into the table, you can do so by rearranging the order via the [(column_name, ...)] option.A Simple Data Import ExampleThis example is based upon the ongoing sales theme. Suppose you want to import a file titledproductreviews.txt, which contains the following information:'43','jason@example.com','I love the new Website!''44','areader@example.com','Why don\'t you sell shoes?''45','anotherreader@example.com','The search engine works great!'The target table, aptly titled product_reviews, consists of three fields, and they are in thesame order (comment_id, email, comment) as the information found in productreviews.txt:LOAD DATA INFILE 'productreviews.txt' INTO TABLE product_reviews FIELDSTERMINATED BY ',' ENCLOSED BY '\'' ESCAPED BY '\\'LINES TERMINATED BY '\n';723

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!