mysqldump导出的表使用mysqlimport恢复提示data too long
发布网友
发布时间:2022-04-21 00:27
我来回答
共1个回答
热心网友
时间:2023-10-28 06:55
可能是编码问题。
1. 建数据库
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\awish>mysql -u root -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24 to server version: 5.0.27-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database test;
2. 设置数据编码为utf8
mysql> use test;
Database changed
mysql> set names utf8; //设置数据库字符集为utf8
Query OK, 0 rows affected (0.00 sec)
3. 创建数据表
mysql> CREATE TABLE person
-> (
-> id INT(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> name varchar(16) default 'nobody',
-> birthday char(19),
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8//创建数据表,字符集设定为utf8
-> ;
Query OK, 0 rows affected (0.03 sec)
4. 创建导入SQL脚本文件 c:\test.sql
use test;
insert into person values(null, '张三', '1984-08-20');
insert into person values(null, '李四', '1984-08-20');
insert into person values(null, '王五', '1984-08-20');
5. 导入SQL脚本文件
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\awish>mysql -u root -p test<C:\test.sql
Enter password: *******
ERROR 1406 (22001) at line 3: Data too long for column 'name' at row 1
C:\Documents and Settings\awish>
分析: 很明显, 张三, 李四, 王五 只有4个字节, 而建表时定义最多可以有15个, 理论下完全可以, 为什么 too long 呢? 唯一的解释就是编码问题!!!!
若把上面SQL脚本改为: insert into person values(null, 'aaaaaaaaaaaaaa', '1984-08-20'); 却可以正常插入!!
后来找资料发现, MySQL的默认编码为 gb2312
在 test.sql 脚本中加入: set names gb2312 问题解决
use test;
set names gb2312;
insert into parent values(null, '张三', '1984-08-20');
insert into parent values(null, '李四', '1984-08-20');
insert into parent values(null, '王五', '1984-08-20');
导入命令:
C:\Documents and Settings\awish>mysql -u root -p test<C:\test.sql
Enter password: *******
C:\Documents and Settings\awish>
导入成功!!!
6. 查询数据
C:\Documents and Settings\awish>mysql -u root -p test
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27 to server version: 5.0.27-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use test;
Database changed
mysql> select * from person;
+----+--------+------------+
| id | name | birthday |
+----+--------+------------+
| 1 | 寮犱笁 | 1984-08-20 |
| 2 | 鏉庡洓 | 1984-08-20 |
| 3 | 鐜嬩簲 | 1984-08-20 |
+----+--------+------------+
3 rows in set (0.00 sec)
mysql>
由于中文字符编码为 utf8 后, 人是不认得了, 我们可以导出看其效果!
7. 导出数据库
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\awish>mysqlmp -u root -p test>c:\test2.sql
Enter password: *******
C:\Documents and Settings\awish>
我们把它存为 C:\test2.sql
打开test2.sql 脚本文件, 我们将会看到:
--
-- Table structure for table `person`
--
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(16) default 'nobody',
`birthday` char(19) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `person`
--
LOCK TABLES `person` WRITE;
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` VALUES (1,'张三','1984-08-20'),(2,'李四','1984-08-20'),(3,'王五','1984-08-20');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
UNLOCK TABLES;
数据完全正确!!
这时如果要想把 test2.sql文件导入到数据库中, 按照上面的, 加上: set names gb2312
mysqldump导出的表使用mysqlimport恢复提示data too long
Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 27 to server version: 5.0.27-community-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> use test;Database changed mysql> select * from person;+-...