日期:2014-05-16  浏览次数:20408 次

从文件导入数据到MySQL数据库
插入方法:INSERT INTO `some_table` (column1,column2,...) values(data1,data2,...),(data3,data4)...;

load data用法:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'

    [REPLACE | IGNORE]

    INTO TABLE tbl_name

    [FIELDS      //字段

        [TERMINATED BY 'string']  //字段间分割符

        [[OPTIONALLY] ENCLOSED BY 'char']//括起字段的符号

           [ESCAPED BY 'char' ]       //字段中的转义符   

    ]

    [LINES

        [STARTING BY 'string']    //行的开始字符串     

        [TERMINATED BY 'string']  //行末尾分割符号   

    ]

    [IGNORE number LINES]          //跳过前面number行

    [(col_name_or_user_var,...)] //字段名称

    [SET col_name = expr,...)]



比如一个数据文本如下:

  股票代码       股票名称    开盘价    收盘价

Def:"000001","深发展A","13.24","13.56"

Def:"000002","万科A","24.43","24.77"



用两种方法导入mysql分别为:
1:LOAD DATA LOCAL INFILE 'table_name.txt'

INTO TABLE database_name

FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\'

LINES STARTING BY 'Def'

IGNORE 1 LINES

(stock_code,stock_name,opening,settlement);

2:

mysqlimport --columns=stock_code,stock_name,opening,settlement --fields-terminated-by=, --fields-enclosed-by=" --fields-escaped-by=\ --lines-terminated-by=\r\n -uroot -ppasswd -hlocalhost -P3306 --ignore-lines=1 -L database_name table_name.txt