問題現象:
當向mysql教程5.5插入中文時,會出現類似錯誤
ERROR 1366 (HY000): Incorrect string value: 'xD6xD0xCExC4' for column
問題原因:
Db characterset的字元集設成了latin1
1、先確定資料裡儲存的是否是亂碼:
mysql>select * from yourtable;
查看如果是亂碼的話,就是你插入資料的時候,當前頁面的編碼方式和你mysql的編碼方式不一致。
2、如果排除了以上亂碼的可能,也就是說資料庫教程中能正常儲存中文,就需要檢查你頁面顯示的編碼方式了
解決方案:
找到MYSQL安裝目錄下的my.ini檔案修改:
[client]
port=3306
[mysql]
default-character-set=gbk
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
參考my.cnf
修改mysql的預設字元集是通過修改它的設定檔來實現的
windows下的mysql設定檔是my.ini,一般在安裝目錄下(如C:Program FilesMySQLMySQL Server 5.0),可以直接在這個檔案裡面加上
default-character-set=gbk #或gb2312,big5,utf8
然後重新啟動mysql
查看 MySQL 資料庫伺服器字元集,資料庫字元集和用戶端字元集
show variables like '%char%';
character_set_client,用戶端字元集
character_set_database,資料庫字元集
character_set_server,伺服器字元集
2. 查看 MySQL 資料表(table) 的字元集
show table status from tablename like '%countries%';
3. 查看 MySQL 資料列(column)的字元集。
show full columns from tablename;
4. 查看當前安裝的 MySQL 所支援的字元集。
show char set;