LOAD Data Syntax Description:
LOAD DATA infile Syntax
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 easiest way to do it
The code is as follows |
Copy Code |
Mysql>load data local infile "d:/ab.txt" into table Mytbl (name,age); |
MySQL supports data import for the load Data command, which is more efficient than direct inserts, and is 20 times times faster than the INSERT statement, as the official says. The use of the following methods:
The code is as follows |
Copy Code |
Mysql>load data local infile "d:/ab.txt" into table Mytbl (name,age); |
Using the commands above, you can import the contents of the D:/ab.txt file into the table mytbl, where name and age are the fields of the table mytbl, corresponding to the data for each row in the Ab.txt file. If you do not specify –enable-local-infile when compiling the MySQL installation, the following error will be reported when using the above command:
ERROR 1148 (42000): The used command isn't allowed with this MySQL version
There are two solutions, one is to recompile the installation plus the above parameters, but directly with the command line execution, as follows:
The code is as follows |
Copy Code |
Mysql-uroot-proot mydb_name--local-infile=1-e ' load data local infile ' d:/ab.txt to Table Mytbl (name,age) ' |
Test A, import 3 million data, the use of the load data command time spent about 3 minutes, or very good!
Some common errors when importing
System error:
Data truncated for column ' F3 ' at row 1
This may be because the field is defined as a double, and the text file is empty, and the text file is updated to:
abc;cdef;2;
Import again, the error is as follows:
Row 1 doesn ' t contain data for all columns
The reason for the analysis may be that there are 5 fields in the table, and there are 4 fields in the text data source, and a few 1 fields.
Data truncated for column ' F3 ' at row 1,
I understand that the system should be treated as NULL, that is, the default value of this field in the database is "-1",
Solutions
The code is as follows |
Copy Code |
Load data infile ' D:\my_program\Tek_sig\example_data1.txt ' into table table_name FIELDS terminated by '; ' LINES terminated by ' n ' (F1,F2,F3,F4) |