首先,這片文章純粹是我的個人經(jīng)驗之談,適用于我常見的環(huán)境及項目中.
個人建議,數(shù)據(jù)庫字符集盡量使用utf8(html頁面對應(yīng)的是utf-8),以使你的數(shù)據(jù)能很順利的實現(xiàn)遷移,因為utf8字符集是目前最適合于實現(xiàn)多種不同字符集之間的轉(zhuǎn)換的字符集,盡管你在命令行工具上可能無法正確查看數(shù)據(jù)庫中的內(nèi)容,我依然強烈建議使用utf8作為默認字符集.
接下來是完整的一個例子:
1.創(chuàng)建數(shù)據(jù)庫表
mysql>create database if not exists my_db default charset utf8 collate utf8_general_ci;
#注意后面這句話 "collate utf8_general_ci",大致意思是在排序時根據(jù)utf8校驗集來排序
#那么在這個數(shù)據(jù)庫下創(chuàng)建的所有數(shù)據(jù)表的默認字符集都會是utf8了
mysql>create table my_table (name varchar(20) not null default '')type=myisam default charset utf8;
#這句話就是創(chuàng)建一個表了,制定默認字符集為utf8
2.寫數(shù)據(jù)
例子1是通過php直接插入數(shù)據(jù):
a.php
<?php
mysql_connect('localhost','user','password');
mysql_select_db('my_db');
//請注意,這步很關(guān)鍵,如果沒有這步,所有的數(shù)據(jù)讀寫都會不正確的
//它的作用是設(shè)置本次數(shù)據(jù)庫聯(lián)接過程中,數(shù)據(jù)傳輸?shù)哪J字符集
//其他編程語言/接口也類似,例如 .net/c#/odbc
//jdbc則設(shè)置連接字符串為類似"jdbc:mysql://localhost/db?user=user&password=123456&useunicode=true&characterencoding=utf-8"
mysql_query("set names utf8;");
//必須將gb2312(本地編碼)轉(zhuǎn)換成utf-8,也可以使用iconv()函數(shù)
mysql_query(mb_convet_encoding("insert into my_table values('測試');", "utf-8", "gb2312"));
?>
例子是通過頁面提交插入數(shù)據(jù)2:
b.php
<?php
//輸出本頁編碼為utf-8
header("content-type:text/html; charset=utf-8");
mysql_connect('localhost','user','password');
mysql_select_db('my_db');
if(isset($_request['name'))
{
//由于上面已經(jīng)指定本頁字符集為utf-8了,因此無需轉(zhuǎn)換編碼
mysql_query(sprintf("insert into my_table values('%s');", $_request['name']));
}
$q = mysql_query("select * from my_table");
while($r = mysql_fetch_row($q))
{
print_r($r);
}
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<form action="" method="post">
<input type="text" name="name" value="">
<input type="submit" value='submit'>
</form>
自此,使用utf8字符集的完整的例子結(jié)束了.
如果你想使用gb2312編碼,那么建議你使用latin1作為數(shù)據(jù)表的默認字符集,這樣就能直接用中文在命令行工具中插入數(shù)據(jù),并且可以直接顯示出來.而不要使用gb2312或者gbk等字符集,如果擔(dān)心查詢排序等問題,可以使用binary屬性約束,例如:
create table my_table ( name varchar(20) binary not null default '')type=myisam default charset latin1;
附1:舊數(shù)據(jù)升級辦法
以原來的字符集為latin1為例,升級成為utf8的字符集。原來的表: old_table (default charset=latin1),新表:new_table(default charset=utf8)。
第一步:導(dǎo)出舊數(shù)據(jù)
mysqldump --default-character-set=latin1 -hlocalhost -uroot -b my_db --tables old_table > old.sql
第二步:轉(zhuǎn)換編碼(類似unix/linux環(huán)境下)
iconv -t utf-8 -f gb2312 -c old.sql > new.sql
或者可以去掉 -f 參數(shù),讓iconv自動判斷原來的字符集
iconv -t utf-8 -c old.sql > new.sql
在這里,假定原來的數(shù)據(jù)默認是gb2312編碼。
第三步:導(dǎo)入
修改old.sql,在插入/更新語句開始之前,增加一條sql語句: "set names utf8;",保存。
mysql -hlocalhost -uroot my_db < new.sql
大功告成!!
附2:支持查看utf8字符集的mysql客戶端有
1.) mysql-front,據(jù)說這個項目已經(jīng)被mysql ab勒令停止了,不知為何,如果國內(nèi)還有不少破解版可以下載(不代表我推薦使用破解版 :-p)。
2.) navicat,另一款非常不錯的mysql客戶端,漢化版剛出來,還邀請我試用過,總的來說還是不錯的,不過也需要付費。
3.) phpmyadmin,開源的php項目,非常好。
4.) linux下的終端工具(linux terminal),把終端的字符集設(shè)置為utf8,連接到mysql之后,執(zhí)行 set names utf8; 也能讀寫utf8數(shù)據(jù)了。
附3:直接使用mysql提供的 alter 語法轉(zhuǎn)換字符集
這對廣大非utf8又想轉(zhuǎn)成utf8的用戶來說,是個天大的喜訊,我也是在學(xué)習(xí)mysql手冊是才發(fā)現(xiàn)的。具體用法如下:
alter table old_table convert to character set charset_name [collate collation_name];
轉(zhuǎn)換之前,記得要先備份舊表,以防萬一。下面是一個實際的例子:
alter table `t_yejr` convert to character set utf8;
這個方法應(yīng)該是從mysql 4.1才開始提供的,大家可以檢查一下自己的版本是否支持,如果不支持,只好按照上面提到的轉(zhuǎn)換了。enjoy it!!!
新聞熱點
疑難解答
圖片精選