MySQL export data select into outfile Usage

This article reprinted from: https://blog.csdn.net/caoxiaohong1005/article/details/72571798


1, select into outfield features:

Export data to a specified directory on the pc.


2, the syntax:

SELECT ... INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        [export_options]

export_options:
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]

Syntax example:

SELECT customer_id, firstname, surname INTO OUTFILE '/exportdata/customers.txt'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM customers;


3, an example of actual operation: the Mac Environment

Built table:

create table testLoadData(
 id bigint(20) not null auto_increment,
 username char(10) not null,
 age tinyint(3) UNSIGNED not null,
 description  text not null,
 primary key(id),
 unique key(username)
)engine=myisam default charset=utf8;

Import Data:

LOAD DATA local INFILE '/Users/xxx/Downloads/loaddata.txt' IGNORE INTO TABLE testLoadData 
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' ignore 1 lines (username, age, description);

Description: xxx native user name

Import data query:

select * from testLoadData




export data:

SELECT  * FROM testLoadData 
INTO OUTFILE '/Users/xxx/Downloads/loaddatass.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

See the corresponding directory: it can be found that the above data has been stored into the loaddatass.txt.

4, encountered a problem:

(1)


Solution: Modify permission to write the corresponding directory under the file:

The input terminal:


(2)


Error reason: sql script file in the path already exists. In fact the files in the MySQL sql script should be to create a file of its own, rather than to write a file that already exists, so a re-write is not in the directory of the file name.

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

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