向mysql 插入中文數值報錯
報錯:Error 1366: Incorrect string value: '\xE6\xB5\x8B\xE8\xAF\x95' for column 'description' at row 1
原因是資料庫欄位沒有設定編碼為utf8而是預設的lanti
(1)查看錶中的各欄位的屬性: show full columns from table_name;
如果看到每個欄位都是lati, 則執行以下語句:
alter table table_name convert to character set utf8 collate utf8_general_ci;
(2)同時檢查一些變數collation_connection、collation_database、collation_server是否都是utf8_general_ci
檢查的語句為:show variable like 'collation%';
修改的語句為:set collation_connection=utf8_general_ci
(3)同時檢查一些變數character的相關變數是否為utf8_general_ci
檢查的語句為:show variable like 'character%';
修改語句如:set character_set_database=utf8;
---------------------------------------------------------------------------------------------------------------------------------------------------------------
檢視資料庫編碼
show variables
like
'%char%'
;
修改資料庫編碼格式為 utf8
alter
database
<資料庫名>
character
set
utf8;