我在CU写的,现在搬过来。CSDN的博客格式终于好了!
LOAD DATA INFILE 一直被认为是MySQL很强大的一个数据导入工具,因为他速度非常的快。
不过有几个问题一定要注意1、编码。2、灵活导入导出。我来举两个例子说明一下。
一、关于编码我们的示例文本文件:
"我爱你","20","相貌平常,经常耍流氓!哈哈""李奎","21","相貌平常,经常耍流氓!哈哈""王二米","20","相貌平常,经常耍流氓!哈哈""老三","24","很强""老四","34","XXXXX""老五","52","***%*¥*¥*¥*¥""小猫","45","中间省略。。。""小狗","12","就会叫""小妹","21","PP的很""小坏蛋","52","表里不一""上帝他爷","96","非常英俊""MM来了","10","。。。""歌颂党","20","社会主义好""人民好","20","的确是好""老高","10","学习很好""斜三","60","眼睛斜了""中华之子","100","威武的不行了""大米","63","我爱吃""苹果","15","好吃"我们的示例表结构:
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| t0 | 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=utf8 | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)我们把这个文本文件从WINDOWS 下COPY到LINUX下看看
mysql> 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`);Query OK, 19 rows affected (0.01 sec)
Records: 19 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from t0;+----+----------+-----+----------------------------+
| id | name | age | description |
+----+----------+-----+----------------------------+
| 1 | 我爱你 | 20 | 相貌平常,经常耍流氓!哈哈 |
| 2 | 李奎 | 21 | 相貌平常,经常耍流氓!哈哈 |
| 3 | 王二米 | 20 | 相貌平常,经常耍流氓!哈哈 |
| 4 | 老三 | 24 | 很强 |
| 5 | 老四 | 34 | XXXXX |
| 6 | 老五 | 52 | ***%*¥*¥*¥*¥ |
| 7 | 小猫 | 45 | 中间省略。。。 |
| 8 | 小狗 | 12 | 就会叫 |
| 9 | 小妹 | 21 | PP的很 |
| 10 | 小坏蛋 | 52 | 表里不一 |
| 11 | 上帝他爷 | 96 | 非常英俊 |
| 12 | MM来了 | 10 | 。。。 |
| 13 | 歌颂党 | 20 | 社会主义好 |
| 14 | 人民好 | 20 | 的确是好 |
| 15 | 老高 | 10 | 学习很好 |
| 16 | 斜三 | 60 | 眼睛斜了 |
| 17 | 中华之子 | 100 | 威武的不行了 |
| 18 | 大米 | 63 | 我爱吃 |
| 19 | 苹果 | 15 | 好吃 |
+----+----------+-----+----------------------------+
19 rows in set (0.00 sec)
我来说明一下相关的参数关于我的导入语句,我现在直说两个,其他的参考手册。
character set gbk;这个字符集一定要写,要不然就会乱码或者只导入一部分数据。
ignore into table 因为name 列加了唯一索引,加这个是为了避免重复数据插入报错。
加入我们再次运行这个导入语句就会发现
Query OK, 0 rows affected (0.00 sec)
Records: 19 Deleted: 0 Skipped: 19 Warnings: 0
没有任何值导入,因为里面已经有了相同的值。
这里也可以用
replace into tableMySQL会把相同的先干掉,再插入新的值。
mysql> load data infile '/tmp/t0.txt' replace into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (`name`,`age`,`description`);Query OK, 38 rows affected (0.00 sec)
Records: 19 Deleted: 19 Skipped: 0 Warnings: 0
mysql> select * from t0;+----+----------+-----+----------------------------+
| id | name | age | description |
+----+----------+-----+----------------------------+
| 20 | 我爱你 | 20 | 相貌平常,经常耍流氓!哈哈 |
| 21 | 李奎 | 21 | 相貌平常,经常耍流氓!哈哈 |
| 22 | 王二米 | 20 | 相貌平常,经常耍流氓!哈哈 |
| 23 | 老三 | 24 | 很强 |
| 24 | 老四 | 34 | XXXXX |
| 25 | 老五 | 52 | ***%*¥*¥*¥*¥ |
| 26 | 小猫 | 45 | 中间省略。。。 |
| 27 | 小狗 | 12 | 就会叫 |
| 28 | 小妹 | 21 | PP的很 |
| 29 | 小坏蛋 | 52 | 表里不一 |
| 30 | 上帝他爷 | 96 | 非常英俊 |
| 31 | MM来了 | 10 | 。。。 |
| 32 | 歌颂党 | 20 | 社会主义好 |
| 33 | 人民好 | 20 | 的确是好 |
| 34 | 老高 | 10 | 学习很好 |
| 35 | 斜三 | 60 | 眼睛斜了 |
| 36 | 中华之子 | 100 | 威武的不行了 |
| 37 | 大米 | 63 | 我爱吃 |
| 38 | 苹果 | 15 | 好吃 |
+----+----------+-----+----------------------------+
19 rows in set (0.00 sec)
(`name`,`age`,`description`);这些也就是具体的表属性了,指明这个就可以导入想要的数据。
2、关于灵活性,其实也就是一个记录功能如果想在导入的时候记录一下导入的具体时间怎么办?
我们来看看
先加一个时间属性记录导入时间。
mysql> alter table t0 add update_time timestamp not null;Query OK, 19 rows affected (0.00 sec)
Records: 19 Duplicates: 0 Warnings: 0
干掉唯一索引
mysql> alter table t0 drop index idx_name;Query OK, 19 rows affected (0.00 sec)
Records: 19 Duplicates: 0 Warnings: 0
mysql> load data infile '/tmp/t0.txt' into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (`name`,`age`,`description`) set update_time=current_timestamp;Query OK, 19 rows affected (0.00 sec)
Records: 19 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from t0;+----+----------+-----+----------------------------+---------------------+
| id | name | age | description | update_time |
+----+----------+-----+----------------------------+---------------------+
| 20 | 我爱你 | 20 | 相貌平常,经常耍流氓!哈哈 | 0000-00-00 00:00:00 |
…………
| 24 | 老四 | 34 | XXXXX | 0000-00-00 00:00:00 |
| 25 | 老五 | 52 | ***%*¥*¥*¥*¥ | 0000-00-00 00:00:00 |
…………
| 35 | 斜三 | 60 | 眼睛斜了 | 0000-00-00 00:00:00 |
| 36 | 中华之子 | 100 | 威武的不行了 | 0000-00-00 00:00:00 |
…………
| 60 | 王二米 | 20 | 相貌平常,经常耍流氓!哈哈 | 2008-06-30 14:58:37 |
…………
| 68 | 上帝他爷 | 96 | 非常英俊 | 2008-06-30 14:58:37 |
| 69 | MM来了 | 10 | 。。。 | 2008-06-30 14:58:37 |
…………
| 75 | 大米 | 63 | 我爱吃 | 2008-06-30 14:58:37 |
| 76 | 苹果 | 15 | 好吃 | 2008-06-30 14:58:37 |
+----+----------+-----+----------------------------+---------------------+
38 rows in set (0.00 sec)
新导入的19条记录时间被记录了下来。
只是之前的数据库没有记录,不过现在不需要这些重复数据了。
干掉他就可以了
mysql> alter table t0 order by id desc;Query OK, 38 rows affected (0.01 sec)
Records: 38 Duplicates: 0 Warnings: 0
mysql> alter ignore table t0 add unique index idx_name (`name`);Query OK, 38 rows affected (0.00 sec)
Records: 38 Duplicates: 19 Warnings: 0
mysql> alter table t0 order by id asc;Query OK, 19 rows affected (0.01 sec)
Records: 19 Duplicates: 0 Warnings: 0
mysql> select * from t0;+----+----------+-----+----------------------------+---------------------+
| id | name | age | description | update_time |
+----+----------+-----+----------------------------+---------------------+
| 58 | 我爱你 | 20 | 相貌平常,经常耍流氓!哈哈 | 2008-06-30 14:58:37 |
| 59 | 李奎 | 21 | 相貌平常,经常耍流氓!哈哈 | 2008-06-30 14:58:37 |
| 60 | 王二米 | 20 | 相貌平常,经常耍流氓!哈哈 | 2008-06-30 14:58:37 |
| 61 | 老三 | 24 | 很强 | 2008-06-30 14:58:37 |
| 62 | 老四 | 34 | XXXXX | 2008-06-30 14:58:37 |
| 63 | 老五 | 52 | ***%*¥*¥*¥*¥ | 2008-06-30 14:58:37 |
| 64 | 小猫 | 45 | 中间省略。。。 | 2008-06-30 14:58:37 |
| 65 | 小狗 | 12 | 就会叫 | 2008-06-30 14:58:37 |
| 66 | 小妹 | 21 | PP的很 | 2008-06-30 14:58:37 |
| 67 | 小坏蛋 | 52 | 表里不一 | 2008-06-30 14:58:37 |
| 68 | 上帝他爷 | 96 | 非常英俊 | 2008-06-30 14:58:37 |
| 69 | MM来了 | 10 | 。。。 | 2008-06-30 14:58:37 |
| 70 | 歌颂党 | 20 | 社会主义好 | 2008-06-30 14:58:37 |
| 71 | 人民好 | 20 | 的确是好 | 2008-06-30 14:58:37 |
| 72 | 老高 | 10 | 学习很好 | 2008-06-30 14:58:37 |
| 73 | 斜三 | 60 | 眼睛斜了 | 2008-06-30 14:58:37 |
| 74 | 中华之子 | 100 | 威武的不行了 | 2008-06-30 14:58:37 |
| 75 | 大米 | 63 | 我爱吃 | 2008-06-30 14:58:37 |
| 76 | 苹果 | 15 | 好吃 | 2008-06-30 14:58:37 |
+----+----------+-----+----------------------------+---------------------+
19 rows in set (0.00 sec)
现在是达到了目的了,为啥中途要干掉唯一索引呢?因为set 语法 再有IGNORE 的时候会忽略掉。
分享到:
相关推荐
如果是导入有中文的数据,我的mysql 设置的utf8 字符集,所以你要导入的xxx.txt 文件也要保存utf-8的字符集,命令 load data infile “d:/Websites/Sxxxx/test1.txt” ignore into table `names` fields...
/usr/local/mysql/bin/mysql -uroot -proot test -e “LOAD DATA INFILE ‘/usr/1.txt’ replace INTO TABLE test FIELDS TERMINATED BY ‘\t’ (name,address)” info: 1.txt zhangsan wuhan lishi wuhan 把1.txt...
(还有sqlimport和LOAD DATA INFILE等导入方法,不过它们主要用于导入.csv或.xml文件数据,不是.sql文件) 假设我们有一个 users.sql 大文件,为方便我们将其拆分成:user1.sql、user2.sql、user3.sql 三个独立的小...
从机需要这些文件来继续复制中断的LOAD DATA INFILE操作。 如果必须恢复MyISAM表,先使用REPAIR TABLE或myisamchk -r来恢复。99.9%的情况下该方法可以工作。如果myisamchk失败,试试下面的方法。请注意只有用--log-...
mysql> LOAD DATA LOCAL INFILE "mytable.txt" INTO TABLE pet; 再使用如下命令看看是否已将数据输入到数据库表中: mysql> select * from mytable; 上篇我们学会了如何创建一个数据库和数据库表,并知道如何...
You can find complete help in 'help.hlp' file ---------------------------------------------------------------------- Radmin ---------------------------------------------------------------------- ...
Exjs相关例子初学者可以看看,代码有英文介绍为:The samples can be browsed by opening the examples folder in a web browser. The samples home page (samples.html) along with some of the individual sample...
首先,我们来看一份真实案例的输出日志: AndroidRuntime: FATAL EXCEPTION: main AndroidRuntime: Process: com.cmccpoc, PID: 2708 AndroidRuntime: java.lang.UnsatisfiedLinkError: Couldn’t load airtalkee ...
static inline void NF_Write(u8 data) { S3C2410_NAND * const nand = S3C2410_GetBase_NAND(); nand->NFDATA = data; } static inline u8 NF_Read(void) { S3C2410_NAND * const nand = S3C2410_GetBase...
mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE; 9:导入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/mysql.sql; 10:删除表 mysql>drop TABLE MYTABLE; 11:清空表 ...
mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt”INTO TABLE MYTABLE; 9:导入.sql文件命令(例如D:/mysql.sql) mysql>use database; mysql>source d:/mysql.sql; 10:删除表 mysql>drop TABLE MYTABLE; 11:...
// Load language file in directory $langFile = rtrim($langDir, '/') . '/' . $lang . '.php'; if (stream_resolve_include_path($langFile) ) { $langMessages = include $...
public void LoadData() { _classList = new ArrayList(); _addressList = new ArrayList(); string content = CFileOperation.ReadFile(CConst.DATAFILE); if (content.Length ) return; string[] ss1 =...
present.LoadData(PageIndex,12,false); } /* * MVP模式的相关状态 * * */ @Override public void showProgress() { progress.showLoading(); } @Override public void hideProgress() { progress....
$data = 'the contents of my file..........'; $this->zip->add_data($name, $data); $this->zip->archive('c:/my_backup.zip');运行这些代码,你会在你的 C 盘根目录下找到一个压缩文件,解压后即为原始文件。 你...
上面我们已经简要的讲述了一些XML的基本原理,我们来看看它们之间的关联以及它们是如何工作的,先看这里一张图: 1.XML描述数据类型。例如:"King lear"是一个标题元素; 2.CSS储存并控制元素的显示样式。例如...
-ue embed file data in UDF extent entry -uf embed UDF FID entries -uv UDF Video Zone compatibility enforced -b "El Torito" 開機區段檔案,不佔檔案空間 (e.g. -bc:\location\cdboot.bin) -p Platform ...
-ue embed file data in UDF extent entry -uf embed UDF FID entries -uv UDF Video Zone compatibility enforced -b "El Torito" 開機區段檔案,不佔檔案空間 (e.g. -bc:\location\cdboot.bin) -p Platform ...
[+] Starting Load_File Fuzzer... [+] Number of system files to be fuzzed: 37 [!] Found /et@c/pa@sswd [!] http://www.rayner.com/products.php?id=22/**/AND/**/1=2/**/UNION/**/SELECT/**/1,LOAD_FILE...
.net 2.0 访问Oracle --与Sql Server的差异,注意事项,常见异常 Ado.net 与NHibernate的关系? 动态创建数据库 SQL Server数据库安全规划全攻略 .net通用数据库访问组件SQL Artisan应用简介1 在Framework1.0下...