Mysql has its own csv engine, which can be used to import csv data to mysql databases, and the speed is much faster than that of batch processing programs written in php or python.
Specific implementation code example:
Copy codeThe Code is as follows: load data infile '/tmp/file.csv' into table _ tablename (set character utf8)
Fields terminated ','
Enclosed '"'
Lines terminated by '\ r \ n ';
Some keywords involved in this Code are explained as follows:
Fields terminated by '': Specifies the field terminator in the csv file, that is, the delimiter between data;
Enclosed by '': Specifies the envelope character;
Lines terminated by '': indicates the line terminator.
The csv format is described in detail in the csv document (RFC4180). The main points are:
(1) fields are separated by commas (,), and data rows are separated by \ r \ n;
(2) The string is enclosed by double quotation marks, and the double quotation marks of the string itself are represented by two double quotation marks.
Through the above explanation, we should have a better understanding of the data import code.
Similarly, csv data can be imported into the mysql database, and data tables in mysql can also be exported to csv files. The exported code example is as follows:Copy codeThe Code is as follows: select * from tablename into outfile '/tmp/data.txt'
Fields terminated ','
Optionally enclosed '"'
Lines terminated by '\ n ';
After exporting data from the database to a file, you must follow the format defined in the exported file before importing the data to the database.