python修改mysql数据库编码

发布时间:2020-07-17编辑:脚本学堂
本文介绍了python修改mysql数据库编码的方法,如何用python脚本修改mysql编码,本实例教大家如何操作,有需要的朋友参考下。

例子,修改mysql/ target=_blank class=infotextkey>mysql数据库编码。
 

复制代码 代码示例:
#-*- 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()