Skip to content

Instantly share code, notes, and snippets.

@ssbarnea
Created July 6, 2011 18:48
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ssbarnea/1068021 to your computer and use it in GitHub Desktop.
Save ssbarnea/1068021 to your computer and use it in GitHub Desktop.
Script to convert a a database to use utf8 encoding
#! /usr/bin/env python
import MySQLdb
host = "localhost"
passwd = ""
user = "root"
dbname = "mydbname"
db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=dbname)
cursor = db.cursor()
cursor.execute("ALTER DATABASE `%s` CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'" % dbname)
sql = "SELECT DISTINCT(table_name) FROM information_schema.columns WHERE table_schema = '%s'" % dbname
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
sql = "ALTER TABLE `%s` convert to character set DEFAULT COLLATE DEFAULT" % (row[0])
cursor.execute(sql)
db.close()
@iurisilvio
Copy link

Thanks, it fixed my problem.

@abhishekgoel137
Copy link

great script thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment