【MySQL】导入数据LOAD DATA INFILE用法说明

语法:

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可以从文本文件中读取数据,并且能够快速导入数据库。

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.

导入文件前,你需要:

  • 被文件导入的数据库表
  • csv文件,里面的数据与数据库表的列的数量和类型对应
  • 账户,需要连接MySQL服务器,需要有file和insert的权限

举个例子:

有以下一张表

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.

命令如下:

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 '" ‘.

列之间 ‘,’ 隔开,用"包含数据。

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

行之间’\n’区分。

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.

因为文件第一行是一些列的名称,不能插进表,所以使用IGNORE 1 ROWS忽略。

参考

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

发布了285 篇原创文章 · 获赞 13 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/105189715