I needed to change collation on 80+ tables so I wrote this simple python script.
I placed the file in my Django project folder (same folder as settings.py etc)
I also changed the character-set on the whole database with the following command:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
The script gets all the tables from the database and then loops trough them running this command for each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8;
Run it like this:
python set_utf_collation.py
set_utf_collation.py
import sysfrom settings import SITE_ROOT
sys.path.append(SITE_ROOT)
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
from django.db import connection
def init():
cursor = connection.cursor()
tables = connection.introspection.table_names()
for table in tables:
print "Fixing table: %s" %table
sql = "ALTER TABLE %s CONVERT TO CHARACTER SET utf8;" %(table)
cursor.execute(sql)
print "Table %s set to utf8"%table
print "DONE!"
init()