How to parse csv data and import it into mysql_PHP tutorial

WBOY
Release: 2016-07-21 15:01:41
Original
708 people have browsed it

mysql has its own csv engine, which can be used to import data in csv into the mysql database, and the speed is much faster than batch processing programs written in php or python.
Specific implementation code example:

Copy code The code is as follows:

load data infile '/tmp/file.csv' into table _tablename (set character utf8)
fields terminated by ','
enclosed by '"'
lines terminated by 'rn';

Some keywords involved in this code are explained as follows:
fields terminated by '': This indicates the field terminator in the csv file, which is the separator between data;
enclosed by '': indicates the envelope character;
lines terminated by '': refers to the line terminator
The csv format is introduced in detail in the csv document (RFC4180). The key points are:
(1) Fields are separated by "," (comma), and data lines are separated by rn;
(2) The string is surrounded by half-width double quotes, and the double quotes of the string itself are
Through the above explanation, you should have a better understanding of the data import code.

Similarly, csv data can be imported into the mysql database, and the data table in mysql can also be imported. Export csv file, exported code example:
Copy code The code is as follows:

select * from tablename into outfile '/tmp /data.txt'
fields terminated by ','
optionally enclosed by '"'
lines terminated by 'n';

When exporting data in the database to After the file is exported, to import the data into the database, the format defined in the exported file must be adhered to.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327962.htmlTechArticlemysql has its own csv engine, which can be used to import data in csv into the mysql database. And the speed is much faster than batch programs written in php or python. Specifically...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!