mysql数据的导出与导入(into outfile和load data file)

导出命令:

mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/runoob.txt'
    -> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    -> LINES TERMINATED BY '\n';

##将passwd这张表的数据全部导出到/tmp/runoob.txt文件中,这个也可以是csv格式


导入命令:

LOAD DATA
    [LOW_PRIORITY | CONCURRENT] [LOCAL]
    INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number {LINES | ROWS}]
    [(col_name_or_user_var
        [, col_name_or_user_var] ...)]
    [SET col_name={expr | DEFAULT}
        [, col_name={expr | DEFAULT}] ...]

##这个是将/tmp目录下的t.csv文件导入到数据库的某张表下(后面与上面的导出时的格式保持一致)
mysql>>load data infile '/tmp/t.csv' into table tb_name FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

##特别说明一下:以上两个命令是在mysql中执行,不是在linux命令行中执行,务必注意!!!!!

上一篇:记录一次使用OpenCsv导入大数据量报错的问题


下一篇:05 RDD编程