将文本内容导入mysql数据库中
导入语句加入字符集,否则乱码
character set gbk;
因为name列加了唯一索列,加这个是为避免重复数据插入报错
ignore into table
把相同的先删除掉
replace into table
[linux]
这里关键
load data infile '/tmp/t0.txt' ignore into table t0
character set gbk fields terminated by ','
enclosed by '"' lines terminated by '\n' (`name`,`age`,`description`);
[windows]
load data infile 'f:/2.txt' ignore into table t0
character set gbk fields terminated by ','
enclosed by '"' lines terminated by '\r\n' (`name`,`age`,`description`);
CREATE TABLE `t0` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`name` char(20) NOT NULL,
`age` tinyint(3) unsigned NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
导入的文件
2.txt
"我爱你1","201","相貌平常,经常耍流氓!哈哈"
"我爱你2","202","相貌平常,经常耍流氓!哈哈"
"我爱你3","203","相貌平常,经常耍流氓!哈哈"
"我爱你4","204","相貌平常,经常耍流氓!哈哈"