1.安裝mysql時設定字元集
mysql資料庫安裝的時候。選擇語言為utf8或者GBK,這樣在資料庫中才可以存放中文。
2.建立表時設定字元集
例如:
CREATE TABLE t_department (
sid varchar(32) NOT NULL,
pid varchar(32) NOT NULL,
thedata varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.在表中更改欄位字元集屬性
以users表為例: 1.查看錶屬性
mysql> show create table users; +-------+----------------------------------------------------------------------- ------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------- ------------------------------------------------------------------------------+ | users | CREATE TABLE `users` ( `userid` int(11) default NULL, `username` char(20) character set latin1 default NULL ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +-------+----------------------------------------------------------------------- ------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
2.表中有資料時會報錯,所以更改username字元集的操作不能成功,需要清空users表中的資料
mysql> truncate table users; Query OK, 3 rows affected (0.01 sec)
3.重新更改user表中username的字元集
mysql> alter table users modify username char(20) character set gbk; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
4.最好重啟mysql,然後就可以插入中文
mysql> insert into users values(88,'中文'); Query OK, 1 row affected (0.01 sec) mysql> select * from users; +--------+----------+ | userid | username | +--------+----------+ | 88 | 中文 | +--------+----------+ 1 row in set (0.00 sec)
來源:http://blog.csdn.net/coder_chang/article/details/54007143