mysql中解決中文插入問題

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.