Skip to content

Instantly share code, notes, and snippets.

@tokiwatch
Last active August 29, 2015 14:08
Show Gist options
  • Save tokiwatch/ae3beefa16226684ceaf to your computer and use it in GitHub Desktop.
Save tokiwatch/ae3beefa16226684ceaf to your computer and use it in GitHub Desktop.
Fix double encoded database.
### このシェルスクリプトは、Latin1のテーブルにUTF8でコンテンツが書き込まれてしまったデータベースを修復します。
my_db='dbname';
my_user='dbuser';
my_pw='dbpassword';
for my_table in $(mysql -u$my_user -p$my_pw -N -s -r $my_db -e 'show tables;'); do
for my_column in $(mysql -u$my_user -p$my_pw -N -r -s -e "use $my_db; show columns from $my_table" | cut -f1); do
echo $my_column;
mysql -u$my_user -p$my_pw -N -r -s -e "use $my_db; UPDATE $my_table SET $my_column = CONVERT(CAST(CONVERT($my_column USING latin1) AS BINARY) USING utf8);"
sleep 1
done
mysql -u$my_user -p$my_pw -N -r -s -e "use $my_db; ALTER TABLE $my_table CHARSET=utf8;"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment