這篇文章主要介紹“怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問(wèn)題”,在日常操作中,相信很多人在怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問(wèn)題問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么解決MySQL命令窗口中文亂碼或插入中文數(shù)據(jù)失敗的問(wèn)題”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、蘿北網(wǎng)站維護(hù)、網(wǎng)站推廣。在Windwos上使用命令窗口操作MySQL數(shù)據(jù)庫(kù)時(shí),如果命令窗口的字符編碼與數(shù)據(jù)庫(kù)不一致時(shí),就會(huì)出現(xiàn)中文亂碼的情況
比如查詢會(huì)出現(xiàn)中文亂碼,或者插入數(shù)據(jù)會(huì)出現(xiàn)錯(cuò)誤“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)
這時(shí)可以先查看下環(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且不能更改
這時(shí)我們可以使用“set names gbk;”來(lái)指定客戶端發(fā)送給mysql語(yǔ)句時(shí)的編碼格式,并且可以看到client、connection、results三個(gè)變量的編碼格式變?yōu)榱薵bk,其它的沒(méi)有變化
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í)行下插入操作沒(méi)問(wèn)題了,查詢數(shù)據(jù)中文也不會(huì)亂碼了
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ù)失敗的問(wèn)題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!