標籤:
最近正在學習mysql,安裝環境是windows server 2003 32位作業系統+mysql 5.1.47
同時也安裝了mysql命令列以及mysql workbench
這裡是test資料庫
create table fortest(empno int auto_increment,empname nvarchar(20) not null,constraint pk_fortest primary key(empno))default charset=utf8;
然後插入了一條資料
set names character utf8;
insert into fortest(empno)values(‘歐陽菲菲‘);
發現插入不了,總是報錯,具體錯誤如下:
這種情況主要是由於預設編碼設定錯誤引起的。
現在就查看一下mysql伺服器的配置情況吧。
具體命令就是
show variables like ‘character%‘;
查出的結果如下:
具體解決方案:
使用MySQL命令列可以實現許多我們需要的功能,不過在使用MySQL命令列的時候,有一個問題是在MySQL命令列插入中文資料或者查詢中文資料時出現亂碼,或者顯示不對。
在MySQL命令列輸入:show variables like ‘character_set_%’;
查看當前配置的編碼;
在MySQL命令列輸入 :set names utf8;
則等同於
set character_set_client=utf8;
set character_set_results=utf8;
set character_set_connection=utf8;
這三條命令
在命令列插入資料之前
執行命令set names gbk;或者set names gb2312;
再插入
查詢資料之前
執行命令set names gbk;或者set names gb2312;
完了之後再set names utf8;
這樣就能正常插入和查詢中文並且保持資料庫編碼為utf8
對於mysql workbench中的查詢出現中文亂碼主要是由於mysql伺服器的配置中預設設定的編碼不是utf8引起的,具體在windows作業系統下,要修改mysql安裝目錄下的my.ini
(linux下則是安裝目錄下的my.cnf檔案)
主要修改的部分如下:
[client]
port=3306
[mysql]
default-character-set=utf8
[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="D:/Program Files/MySQL/MySQL Server 5.1/"
#Path to the database root
datadir="D:/MySQL/MySQL Server 5.1/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=utf8
具體修改的部分就是紅色粗體字的文字部分。
轉載自:http://blog.sina.com.cn/s/blog_8a3a624901018j83.html
mysql命令列以及mysql workbence查詢結果中文亂碼的解決方案