本文共 3582 字,大约阅读时间需要 11 分钟。
数据库中数据的导入导出是一个最简单, 也是最必须的功能.
比如系统在开发时候在个人PC机上, 数据库有很多的初始化数据, 系统开发完成了, 则要把这些初始化数据从PC机上导出成SQL脚本, 在服务器上只要导入这些SQL脚本即可完成数据库初始化.
但是, 对于中文字符的导入却成了大问题, 导入中文, 数据老是报错"Da
这个问题一直困扰着我, 由于以前数据量比较小, 也就不用导入功能, 大不了再重新初始化一次了. 如今不行了, 初始化数据实在太多, 不可能重新初始化, 但我一直认为一定会有办法解决这个问题, 今天上网找资料搞了大半天, 这个问题基本解决.
为了清楚描述这个问题, 现详细讲解如下:
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-ntType '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: DaC:\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>mysqldump -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 da
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
文章出处:
转载地址:http://yrxqb.baihongyu.com/