] [MySQL data import instructions LOAD DATA INFILE

grammar:

load data  [low_priority] [local] infile 'file_name txt' [replace | ignore]
into table tbl_name
[fields
[terminated by't']
[OPTIONALLY] enclosed by '']
[escaped by'\' ]]
[lines terminated by'n']
[ignore number lines]
[(col_name,   )]

The LOAD DATA INFILE statement allows you to read data from a text file and import the file’s data into a database table very fast.

LOAD DATA INFILE can read data from a text file, and can quickly into the database.

Before importing the file, you need to prepare the following:

  • A database table to which the data from the file will be imported.
  • A CSV file with data that matches with the number of columns of the table and the type of data in each column.
  • The account, which connects to the MySQL database server, has FILE and INSERT privileges.

Before importing files, you need:

  • The imported file database table
  • csv file, the number and types of columns of a database table inside the data corresponding to the
  • Account, you need to connect to the MySQL server, you need to have permission to file and insert the

for example:

There are a table

CREATE TABLE discounts (
    id INT NOT NULL AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    expired_date DATE NOT NULL,
    amount DECIMAL(10 , 2 ) NULL,
    PRIMARY KEY (id)
);

The following discounts.csv file contains the first line as column headings and other three lines of data.

Command is as follows:

LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The field of the file is terminated by a comma indicated by FIELD TERMINATED BY ‘,’ and enclosed by double quotation marks specified by ENCLOSED BY '" ‘.

Between the columns '' separated by "contains data.

Each line of the CSV file is terminated by a newline character indicated by LINES TERMINATED BY ‘\n’ .

Between the rows '\ n' distinction.

Because the file has the first line that contains the column headings, which should not be imported into the table, therefore we ignore it by specifying IGNORE 1 ROWS option.

Because the first line of the file is the name of a series can not be inserted into the table, so use IGNORE 1 ROWS ignored.

reference

https://www.mysqltutorial.org/import-csv-file-mysql-table/

https://www.iteye.com/blog/hunan-752606

https://www.jianshu.com/p/bcafd8f3ad8e

https://blog.csdn.net/caoxiaohong1005/article/details/72570147

Published 285 original articles · won praise 13 · views 80000 +

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105189715