How to parse CSV data into MySQL _php tips

Source: Internet
Author: User
MySQL itself has a CSV engine that allows you to import data from a CSV into a MySQL database, faster than a batch program written in PHP or Python.
specific implementation code examples:
Copy Code code as follows:

Load data infile '/tmp/file.csv ' into table _tablename (set character UTF8)
Fields terminated by ', '
Enclosed by ' "'
Lines terminated by ' \ r \ n ';

Some of the key words involved in this code are explained as follows:
Fields terminated by ': This is to indicate the field terminator in the CSV file, which is the delimiter between the data;
Enclosed by ': indicates an envelope character;
Lines terminated by ': line terminator
The CSV format is described in detail in the CSV document (RFC4180), with the following highlights:
(1) The fields are separated by a "," (comma) interval between rows of data using \ r \ n;
(2) The string is surrounded by a half-angle double quotation mark, and the double quotation marks in the string itself are expressed in two double quotes.
Through the above explanation, detailed to the data import code should have a better understanding.

Similarly, the CSV data can be imported into the MySQL database, the data table in MySQL can also export the CSV file, the exported code example:
Copy Code code as follows:

SELECT * FROM tablename into outfile '/tmp/data.txt '
Fields terminated by ', '
Optionally enclosed by ' "'
Lines terminated by ' \ n ';

When you export data from a database to a file, and then import the data into the database, you must follow the format defined in the file that was exported.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.