Txt csv file and import the data into the MySQL database table

This article summarizes the CentOS7 use the LOAD DATA <LOCAL> INFILE statement to MySQL8.0 method txt file and import csv file data and database errors encountered. Because both the same method, we first introduce the import operation and error txt files, csv attach a brief description of the command and direct the final.

Raw data

name owner species sex birth death
Fluffy Harold cat f 1993-02-04  
Claws Gwen cat m 1994-03-17  
Buffy Harold dog f 1989-05-13  
Fang Benny dog m 1990-08-27  
Bowser Diane dog m 1979-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11  
Whistler Gwen bird   1997-12-09  
Slim Benny snake m 1996-04-29  

Prepare txt file

The null value replaced by N \, within each row delimiter data \ T, between lines newline is \ r \ n. Txt file then stored to / var / lib / mysql-files path under.

Prepare data table

mysql> use testdb2;
mysql> create table tbl_pet (
    -> name varchar(10),
    -> owner varchar(10),
    -> species varchar(10),
    -> sex varchar(1),
    -> birth date,
    -> death date)CHARSET = utf8;

Import Data

mysql> LOAD DATA INFILE '/var/lib/mysql-files/pet.txt' INTO TABLE tbl_pet LINES TERMINATED BY '\r\n';

Import Results:

mysql> select * from tbl_pet;                                             
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)

You can continue to import the data into the new table will automatically be appended to the original recording.

 

Error Summary:

#Issue1:ERROR 1148 (42000): The used command is not allowed with this MySQL version

Error statement: MySQL> the LOAD the DATA the LOCAL INFILE '/var/lib/mysql/pet.txt' the INTO TABLE tbl_pet;

Error reason: more than a LOCAL keyword. When the file at the client side write LOCAL, when the server without writing. My MySQL database and txt file is in the same virtual machine, so do not add LOCAL.

 

#Issue2:ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

Error Reason: MySQL path provided secure-file-priv corresponding to the attribute, when the data file is imported from the other paths will be given.

Solution: Check secure-file-priv specify the path and the data file into it. Reference https://blog.csdn.net/FallingU/article/details/75675220

mysql> show variables like '%secure%';
+--------------------------+-----------------------+
| Variable_name            | Value                 |
+--------------------------+-----------------------+
| secure_file_priv         | /var/lib/mysql-files/ |
+--------------------------+-----------------------+

 

#Issue3:ERROR 1292 (22007): Incorrect date value: '' for column 'death' at row 1

Wrong reasons: the beginning of a direct copy of the original data in the table to txt file does not handle, place an empty value is empty, do not meet the representation of null values ​​in MySQL, so an error.

Solution: The empty value \ N instead. Reference https://dev.mysql.com/doc/refman/8.0/en/loading-tables.html

 

Issue4 # : Use LOAD DATA INFILE '/var/lib/mysql-files/pet.txt' INTO TABLEimport data still fails

Error: 'for column' death 'at row 1 date value:' N

Error reason: terminated by the default value of fields is '\ t', lines terminated by default is '\ n', while Windows files on a newline ' \ r \ the n- ', the X-Mac OS is '\ r'.

The solution: add LINES TERMINATED BY '\ r \ n', as follows:

mysql> LOAD DATA INFILE '/var/lib/mysql-files/pet.txt' INTO TABLE tbl_pet LINES TERMINATED BY '\r\n';

 


Import csv file data

Raw data files: source https://blog.csdn.net/u013378642/article/details/80952849

mysql> use testdb;

mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| movie_tbl        |
+------------------+

mysql> desc movie_tbl;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| movie_id    | int(11)          | YES  |     | NULL    |                |
| movie_name  | varchar(40)      | NO   |     | NULL    |                |
| movie_score | float unsigned   | YES  |     | NULL    |                |
| region      | varchar(16)      | YES  |     | NULL    |                |
| year        | year(4)          | YES  |     | NULL    |                |
| movie_type  | varchar(30)      | YES  |     | NULL    |                |
| director    | varchar(80)      | YES  |     | NULL    |                |
| actor       | varchar(60)      | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

mysql> LOAD DATA INFILE '/var/lib/mysql-files/Movies.csv' INTO TABLE movie_tbl
    -> CHARACTER SET utf8
    -> FIELDS TERMINATED BY ','
    -> LINES TERMINATED BY '\r\n'
    -> IGNORE 1 LINES
    -> (movie_id,movie_name,movie_score,region,year,movie_type,director,actor);

Additional information:

1. CHARACTER SET utf8: Because of Chinese data, specify utf8 encoding.

2. FIELDS TERMINATED BY ',': csv file ',' separated data.

3. IGNORE 1 LINES: skipping first row header data in the original file. If you do not write, the header will be treated as data import. If you want to skip multiple lines from scratch, you can change the front of the digital LINES.

4. To import data in the last field, field order and would be consistent in the order of the original document.

 

The End ~

发布了37 篇原创文章 · 获赞 47 · 访问量 10万+

Guess you like

Origin blog.csdn.net/u013378642/article/details/81220809