您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問題”,在日常操作中,相信很多人在怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問題”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
在Windwos上使用命令窗口操作MySQL數(shù)據(jù)庫時,如果命令窗口的字符編碼與數(shù)據(jù)庫不一致時,就會出現(xiàn)中文亂碼的情況
比如查詢會出現(xiàn)中文亂碼,或者插入數(shù)據(jù)會出現(xiàn)錯誤“ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1”
mysql> insert into user(name,gender,age) values('張三','男',22); ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1 mysql> select * from user; +----+----------+--------+-----+---------------------+| id | name | gender | age | updated_time | +----+----------+--------+-----+---------------------+| 1 | zhangsan | M | 22 | 2018-10-31 17:03:25 | | 2 | 寮犱笁 | 鐢? | 22 | 2018-11-01 09:01:43 | +----+----------+--------+-----+---------------------+2 rows in set (0.00 sec)
這時可以先查看下環(huán)境編碼配置,我的是utf8,結(jié)果是下面這樣
mysql> show variables like 'char%'; +--------------------------+---------------------------------------------------------+| Variable_name | Value | +--------------------------+---------------------------------------------------------+| character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ | +--------------------------+---------------------------------------------------------+8 rows in set (0.00 sec)
查看windwos命令窗口的的屬性可知,編碼格式為GBK且不能更改
這時我們可以使用“set names gbk;”來指定客戶端發(fā)送給mysql語句時的編碼格式,并且可以看到client、connection、results三個變量的編碼格式變?yōu)榱薵bk,其它的沒有變化
mysql> set names gbk; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'char%'; +--------------------------+---------------------------------------------------------+| Variable_name | Value | +--------------------------+---------------------------------------------------------+| character_set_client | gbk | | character_set_connection | gbk | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ | +--------------------------+---------------------------------------------------------+8 rows in set (0.00 sec)
再次執(zhí)行下插入操作沒問題了,查詢數(shù)據(jù)中文也不會亂碼了
mysql> insert into user(name,gender,age) values('張三','男',22); Query OK, 1 row affected (0.04 sec) mysql> select * from user; +----+----------+--------+-----+---------------------+| id | name | gender | age | updated_time | +----+----------+--------+-----+---------------------+| 1 | zhangsan | M | 22 | 2018-10-31 17:03:25 | | 2 | 張三 | 男 | 22 | 2018-11-01 09:01:43 | +----+----------+--------+-----+---------------------+2 rows in set (0.00 sec)
到此,關(guān)于“怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。