分享 - mysql big5 轉 utf-8 轉換程式

yeahokay發表於2007-08-20

這支程式 會 dump 出 db 下的所有 insert 的 query ,

使用方法

1.

將 big5 環境的 create dump 出來 ,

然後 再將 CHARSET=latin1 或 CHARSET=big5

改成 CHARSET=utf8

然後 create 一個空的 databases + table .

2.
下載 metabase : 請參考附件

3.
convert.php

[@more@]

#!/usr/bin/php -q
set_time_limit(0) ;
############################################################################
# sql link start
############################################################################
$db_type = "mysql";
$host_name = "localhost" ;
$database_name = "heyforum_dz5" ;
$user_id = "root" ;
$user_password = "xxxxxxxxxxxx" ;
include_once("metabase/metabase_interface.php");
include_once("metabase/metabase_database.php");
$db_params=array(
"Host" =>$host_name
,"Type" =>$db_type
,"User" =>$user_id
,"Password" =>$user_password
,"IncludePath" =>"metabase/"
,"Persistent" =>0
);
############################################################################
# sql link end
############################################################################

$error=MetabaseSetupDatabase($db_params,$db);
MetabaseSetDatabase($db,$database_name);

$query ="show tables";
$db_res=MetabaseQuery($db,$query);
if(!$db_res) {
ecstart_alert_err($msg["system_err"]);
}
$rows=MetabaseNumberOfRows($db,$db_res);
MetabaseGetColumnNames($db,$db_res,$db_field_name);
if($rows)
{
for($row=1;$row<=$rows;$row++)
{
if(is_array($db_field_name)){
foreach($db_field_name as $key => $value){
$table[$row-1]=MetabaseFetchResult($db,$db_res,$row-1,$key) ;
}
}
}
}
if(is_array($table)){
foreach($table as $k => $v){
$query ="describe ".$v;
$db_res=MetabaseQuery($db,$query);
if(!$db_res) {
ecstart_alert_err($msg["system_err"]);
}
$rows=MetabaseNumberOfRows($db,$db_res);
MetabaseGetColumnNames($db,$db_res,$db_field_name);
if($rows)
{
for($row=1;$row<=$rows;$row++)
{
if(is_array($db_field_name)){
foreach($db_field_name as $key => $value){
$field[$v][$row-1]=MetabaseFetchResult($db,$db_res,$row-1,"field") ;
}
}
}
}
//print_r($field);



}
}




if(is_array($field)){
foreach($field as $k => $v){
// get field value
$query ="select * from ".$k;
$db_res=MetabaseQuery($db,$query);
if(!$db_res) {
ecstart_alert_err($msg["system_err"]);
}
$rows=MetabaseNumberOfRows($db,$db_res);
MetabaseGetColumnNames($db,$db_res,$db_field_name);
if($rows)
{
for($row=1;$row<=$rows;$row++)
{
if(is_array($db_field_name)){
foreach($db_field_name as $key => $value){
$field_value[$row-1][$key]=MetabaseFetchResult($db,$db_res,$row-1,$key) ;

}
}
$no = 0 ;
$field_str = "";
foreach($v as $fv){
if($no == 0){
$field_str .= "`".$fv."`";
}
else{
$field_str .= ",`".$fv."`";
}
$no = $no+1 ;
}

$sno = 0 ;
$field_value_str = "";
foreach($field_value[$row-1] as $fvk => $fvv){
if($sno == 0){
$field_value_str .= "'".mysql_escape_string(iconv('BIG5','UTF-8',$fvv))."'";
}
else{
$field_value_str .= ",'".mysql_escape_string(iconv('BIG5','UTF-8',$fvv))."'";
}
$sno = $sno+1 ;
}


$query = "insert into ".$k."(".$field_str.") values(".$field_value_str.");";
unset($field_value[$row-1]);
unset($field_str);
unset($field_value_str);
echo $query . " ";
//print_r($field_value);
}
}

unset($field_value);
}
}





MetabaseCloseSetup($db)

?>

4.
將 convert.php 存檔後

執行 ./convert.php > convert.sql

5. mysql database_name -uroot -p < convert.sql

好了 ccc

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/786540/viewspace-964181/,如需轉載,請註明出處,否則將追究法律責任。

相關文章