无法将包含数据的大型CSV文件转换为mysql数据库[duplicate]

7uzetpgm  于 2022-12-06  发布在  Mysql
关注(0)|答案(1)|浏览(134)

此问题在此处已有答案

How do I import CSV file into a MySQL table?(21个答案)
上个月关门了。
如何将大型txt文件转换为mysql数据库,文件大小为3GB,包含1100万行。文件中的每一行都是这样的:

"1303179444","","","+20******5811","","","Ahmed","Al Emam","male","/ahmed.e.alemam","","ahmed.e.alemam","Ahmed Al Emam","description here","Ahmed Emam Dental Clinic","Dentist","","Dekernes  Ad Daqahliyah  iraq","MUST","ahmed.e.alemam@ahmed.e.alemam","0","0","0","1/1/0001 12:00:00 AM","1/1/0001 12:00:00 AM","","","","","","","","","",""

我试图用逗号分隔并通过php将数据输入到数据库中,但这花了很长时间,整个文件没有保存。我想用另一种方法将其转换为sql文件

xxhby3vn

xxhby3vn1#

您可以使用mysql LOAD DATA INFILE命令完成此任务。
首先创建表:

CREATE TABLE items (
    field1 ...,
    field2 ...,
    field3 ...
    PRIMARY KEY (field1)
);

然后执行以下命令:

LOAD DATA INFILE 'data.csv' # full path to the csv file
INTO TABLE items         
FIELDS TERMINATED BY ','    # adjust to your csv format
ENCLOSED BY '"'             # adjust to your csv format
LINES TERMINATED BY '\n'    # adjust to your csv format
IGNORE 1 ROWS;              # if you have header row in the csv

还可以指定文件中的列与表中的列之间的Map,甚至可以更改。
例如,假设您要Map:

[column1 in table] is [column3 in csv]
[column2 in table] is [column5 in csv]
[column3 in table] is [column1 in csv]
[column4 in table] is 2 * [column6 in csv]
[column5 in table] is the current timestamp
ignore [column2 in csv]
ignore [column4 in csv]

您可以使用以下命令:

LOAD DATA INFILE 'data.csv' # full path to the csv file
INTO TABLE items
FIELDS TERMINATED BY ','    # adjust to your csv format
ENCLOSED BY '"'             # adjust to your csv format
LINES TERMINATED BY '\n'    # adjust to your csv format
IGNORE 1 ROWS.              # if you have header row in the csv
(column3, @skip, column1, @skip, column2, @c4)
SET 
  column4= 2*@c4, 
  column5=CURRENT_TIMESTAMP;

相关问题