Ramblings

May 7, 2008

Snippet to update Django tables to utf8

Filed under: cool, django, orm, python, snippet, tip, tool — michaelangela @ 11:54 pm

So you get the dreaded illegal mix of collations:

Pythoneer » Convert MySQL table to utf8

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

What do you do? Not too bad fortunately. And if you’re working with Django, it’s a little bit easier. Launch the shell with python manage.py shell. Then in the shell do something like this:

from django.db import connection
cursor = connection.cursor()
cursor.execute('SHOW TABLES')
results=[]
for row in cursor.fetchall(): results.append(row)
for row in results: cursor.execute('ALTER TABLE %s CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' % (row[0]))

I’m sure this can be optimized a bit but it got the job done simply enough.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: