`
null_386
  • 浏览: 43312 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

修改mysql数据库编码的python脚本

阅读更多
#-*- coding:utf8 -*-
#统一数据库内容所有表项的编码
import MySQLdb

host = "localhost"
passwd = "xxxx"
user = "xxx"
dbname = "xxx"

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()

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics