Guests can not chat Login

Cara convert massal CHARSET MySQL menjadi UTF8

Tanya jawab tentang script PHP, MySQL, Javascript, dan sejenisnya.

Cara convert massal CHARSET MySQL menjadi UTF8

Postby jawaad » 23 Jan 2010, 22:43

Ada tiga cara untuk mengubah charset data MySQL.
Pilih salah satunya saja.
Menurut pengalaman saya, saya hanya bisa berhasil dengan cara ketiga.
Bagaimana dengan Anda?

Cara Pertama:
Code: Select all
SELECT CONCAT('ALTER TABLE `', t.`TABLE_SCHEMA`, '`.`', t.`TABLE_NAME`, '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') as stmt
FROM 
`information_schema`.`TABLES` t
WHERE 1
AND t
.`TABLE_SCHEMA` = 'database_name'
ORDER BY 1
ket.: ubah tulisan database_name sesuai dengan kebutuhan.
sumber: http://muzso.hu/2008/04/09/how-to-chang ... bles-and-c

Cara Kedua:
Code: Select all
USE information_schema;

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'char', 'binary'), ';') FROM columns 
WHERE table_schema 
= 'nama_database' and data_type LIKE '%char%';

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', REPLACE(column_type, 'text', 'blob'), ';') FROM columns 
WHERE table_schema 
= 'nama_database' and data_type LIKE '%text%';

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns 
WHERE table_schema 
= 'nama_database' and data_type LIKE '%char%';

SELECT CONCAT('ALTER TABLE ', table_name, ' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8;') FROM columns 
WHERE table_schema 
= 'nama_database' and data_type LIKE '%text%';

ALTER DATABASE nama_database CHARACTER SET utf8;

USE nama_database; 
ket.: ubah ke-enam tulisan nama_database sesuai dengan kebutuhan.
sumber: http://www.haidongji.com/2008/11/11/con ... -in-mysql/

Cara Ketiga:
Code: Select all
<?php
// original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql
// improved/modified (v1.04) by Bogdan http://bogdan.org.ua/
// this script will output all queries needed to change all fields/tables to a different collation
// it is HIGHLY suggested you take a MySQL dump/backup prior to running any of the generated queries
// this code is provided AS IS and without any warranty
// add text/plain header when used not as a CLI script; PHP CLI SAPI shouldn't output headers
header("Content-Type: text/plain");
// precaution
die("Make a backup of your MySQL database, then remove this line from the code!");
set_time_limit(0);
// collation you want to change to:
$convert_to   = 'utf8_general_ci';
// character set of new collation:
$character_set= 'utf8';
// DB login information - *modify before use*
$username = 'user';
$password = 'pass';
$database = 'database_name';
$host     = 'localhost';
//-- usually, there is nothing to modify below this line --//
// show TABLE alteration queries?
$show_alter_table = true;
// show FIELD alteration queries?
$show_alter_field = true;
mysql_connect($host, $username, $password);
mysql_select_db($database);
$rs_tables = mysql_query(" SHOW TABLES ") or die(mysql_error());
while ($row_tables = mysql_fetch_row($rs_tables)) {
    $table = mysql_real_escape_string($row_tables[0]);
    // Alter table collation
    // ALTER TABLE `account` DEFAULT CHARACTER SET utf8
    if ($show_alter_table)
        echo("ALTER TABLE `$table` DEFAULT CHARACTER SET $character_set;\n");
    $rs = mysql_query(" SHOW FULL FIELDS FROM `$table` ") or die(mysql_error());
    while ( $row = mysql_fetch_assoc($rs) ) {
        if ( $row['Collation'] == '' || $row['Collation'] == $convert_to )
            continue;
        // Is the field allowed to be null?
        if ( $row['Null'] == 'YES' )
            $nullable = ' NULL ';
        else
            $nullable 
= ' NOT NULL';
        // Does the field default to null, a string, or nothing?
        if ( $row['Null'] == 'YES' && $row['Default'] === NULL )
            $default = " DEFAULT NULL";
        elseif ( $row['Default'] != '' )
            $default = " DEFAULT '".mysql_real_escape_string($row['Default'])."'";
        else
            $default 
= '';
        // Alter field collation:
        // ALTER TABLE `tab` CHANGE `fiel` `fiel` CHAR( 5 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
        if ($show_alter_field) {
            $field = mysql_real_escape_string($row['Field']);
            echo "ALTER TABLE `$table` CHANGE `$field` `$field` $row[Type] CHARACTER SET $character_set COLLATE $convert_to $nullable $default;\n";
        }
    }
}
?>
lalu, copy paste hasil output perintah SQL ke phpmyadmin dan execute.
sumber: http://bogdan.org.ua/2008/02/08/convert ... other.html
(script ini saya ubah sedikit sesuai dengan rekomendasi dr komentar website sumber tsbt sehingga bisa berhasil, dan di website sumbernya sampai skrng masih blm di fix-kan)

selamat mencoba.. :mrgreen:
jawaad
 
Posts: 81
Joined: 12 Dec 2009, 18:57

Return to Script PHP MySQL Javascript

Who is online

Users browsing this forum: No registered users and 0 guests

cron