导入数据(路径不能有中文)
load data local infile 'D:\\ttt.txt' into table t_weibo_big FIELDS TERMINATED BY '\t';
如出错The used command is not allowed with this MySQL version可用
mysql -uroot -proot db_race --local-infile=1 -e 'load data local infile "/home/nlp/yj/data/race/position.csv" into table t_position FIELDS TERMINATED BY "," ignore 1 lines (positionID, sitesetID, positionType);'
导出数据
SELECT text FROM t_weibo_big_filtered INTO OUTFILE 'd:\\weibo_text.txt' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
导入数据,忽略第一行
/*
user
userID,age,gender,education,marriageStatus,haveBaby,hometown,residence
*/
mysql -uroot -proot db_race --local-infile=1 -e 'load data local infile "/home/nlp/yj/data/race/user.csv" into table t_user FIELDS TERMINATED BY "," ignore 1 lines (userID,age,gender,education,marriageStatus,haveBaby,hometown,residence);'
Excel 保存为 Book1.csv 以后,内容为:
编号,名称,说明
1,测试数据1,"测试CSV文件中,有逗号"
2,测试数据2,"测试CSV文件中有""双引号"""
3,测试数据3,"测试CSV文件中,有逗号和""双引号"""
4,测试数据4,普通数据
建测试目标表
MySQL> CREATE TABLE Test_Book1 (
-> id int,
-> name VARCHAR(10),
-> data VARCHAR(100)
-> );
Query OK, 0 rows affected (0.05 sec)
下面的 lines terminated by '\r\n' 是 要求换行符号,为 windows的换行
下面的 ignore 1 lines是 忽略第一行的标题行。
mysql> LOAD DATA INFILE 'f:/Book1.csv'
-> INTO TABLE Test_Book1
-> FIELDS TERMINATED BY ','
-> OPTIONALLY ENCLOSED BY '"'
->lines terminated by '\r\n'
-> ignore 1 lines
-> (id, name, data);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test_book1;
+------+-----------+--------------------------------+
| id | name | data |
+------+-----------+--------------------------------+
| 1 | 测试数据1 | 测试CSV文件中,有逗号 |
| 2 | 测试数据2 | 测试CSV文件中有"双引号" |
| 3 | 测试数据3 | 测试CSV文件中,有逗号和"双引号" |
| 4 | 测试数据4 | 普通数据 |
+------+-----------+--------------------------------+
4 rows in set (0.00 sec)