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"
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);
下面看一下我的实际操作:
如果你登陆了远程的mysql服务器,然后,想把你本地的文本导入到数据库中的时候,需要local,命令不同以上
load data local infile '/usr/local/mysql/var/Statistic/Spam.txt' into table Spam;