MySQL中匯入中文字元解決方案

2021-06-10 03:06:24 字數 4258 閱讀 4381

資料庫中資料的匯入匯出是乙個最簡單, 也是最必須的功能.

比如系統在開發時候在個人pc機上, 資料庫有很多的初始化資料, 系統開發完成了, 則要把這些初始化資料從pc機上匯出成sql指令碼, 在伺服器上只要匯入這些sql指令碼即可完成資料庫初始化.

但是, 對於中文字元的匯入卻成了大問題, 匯入中文, 資料老是報錯"data too long for column xx at row n"

這個問題一直困擾著我, 由於以前資料量比較小, 也就不用匯入功能, 大不了再重新初始化一次了. 如今不行了, 初始化資料實在太多, 不可能重新初始化, 但我一直認為一定會有辦法解決這個問題, 今天上網找資料搞了大半天, 這個問題基本解決.

為了清楚描述這個問題, 現詳細講解如下:

1. 建資料庫

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指令碼檔案

c:\documents and settings\awish>mysql -u root -p testc:\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 testc:\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. 匯出資料庫

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 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

8. 小結

致此, 在mysql中匯入中文字元問題解決, 感謝各位的閱讀.

如有指教和疑問, 請:

祝大家使用 mysql 愉快!

MySQL不能插入中文字元及中文字元亂碼問題

mysql的預設編碼是latin1,不支援中文,要支援中午需要把資料庫的預設編碼修改為gbk或者utf8。在安裝後mysql之後,它的配置檔案不是很給力,不知道你們的是不是,反正我的是!開始插入中文字元的時候出現如下錯誤 error 1366 hy000 incorrect string value...

MySQL不能插入中文字元及中文字元亂碼問題

mysql的預設編碼是latin1,不支援中文,要支援中午需要把資料庫的預設編碼修改為gbk或者utf8。在安裝後mysql之後,它的配置檔案不是很給力,不知道你們的是不是,反正我的是!開始插入中文字元的時候出現如下錯誤 error 1366 hy000 incorrect string value...

mysql擷取中文字元 mysql 擷取中文字元

1 char n 型別 char型別時定長的型別,即當定義的是char 10 輸入的是 abc 這三個字元時,它們佔的空間一樣是10個位元組,包括7個空位元組。當輸入的字元長度超過指定的數時,char會擷取超出的字元。而且,當儲存char值時,mysql是自動刪除輸入字串末尾的空格。char是適合儲...