Django admin 保存unicode报错 <MySQL“incorrect string alue” error > 解决办法
2013-12-19 23:01
423 查看
如题,原因是创建数据库和表的时候 默认字符集设置的问题,修改即可。
下面是批量修改数据库所有表的列的 默认字符集 Python 脚本
MySQL 版本为 5.5,其他版本没有试。
打开MySQL 可以查看修改后的表的字符集
下面是批量修改数据库所有表的列的 默认字符集 Python 脚本
#! /usr/bin/env python import MySQLdb host='localhost' password='your passwd' user='your user name' dbname='your database naem' db=MySQLdb.connect(host=host,user=user,passwd=password,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: #print row[0] sql="ALTER TABLE %s convert to character set DEFAULT COLLATE DEFAULT " % (row[0]) cursor.execute(sql) db.close()python 运行即可。
MySQL 版本为 5.5,其他版本没有试。
打开MySQL 可以查看修改后的表的字符集
mysql> SELECT T.table_name, T.table_collation, CCSA.character_set_name FROM information_schema.`TABLES` T, -> information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA -> WHERE CCSA.collation_name = T.table_collation -> AND T.table_schema = "databasename"; +----------------------------+-----------------+--------------------+ | table_name | table_collation | character_set_name | +----------------------------+-----------------+--------------------+ | auth_group_permissions | utf8_unicode_ci | utf8 | | auth_permission | utf8_unicode_ci | utf8 | | auth_user | utf8_unicode_ci | utf8 | | auth_user_groups | utf8_unicode_ci | utf8 | | auth_user_user_permissions | utf8_unicode_ci | utf8 | | table_name1 | utf8_unicode_ci | utf8 | | table_name2 | utf8_unicode_ci | utf8 | | table_name3 | utf8_unicode_ci | utf8 | | table_name4 | utf8_unicode_ci | utf8 | | django_admin_log | utf8_unicode_ci | utf8 | | django_content_type | utf8_unicode_ci | utf8 | | django_session | utf8_unicode_ci | utf8 | +----------------------------+-----------------+--------------------+ 14 rows in set (0.03 sec)
相关文章推荐
- java 向 mysql 插入汉字 Incorrect string value 解决办法
- 向 mysql 插入insert时 汉字 Incorrect string value 解决办法
- Missing map from Nullable`1 to String. Create using Mapper.CreateMap<Nullable`1, String>. 解决办法
- mysql 插入汉字 Incorrect string value 解决办法
- JSONObject put List<Double> 后转化为String问题的解决办法
- java 向 mysql 插入汉字 Incorrect string value 解决办法
- 解决mysql保存数据SQLException: Incorrect string value: '\xF0\x9F\x91\x87\xE5\x91...' for column '错误
- 无法载入 mysql 扩展,<br />请检查 PHP 配置 解决办法
- PHP中,<textarea>里的内容保存MYSQL后,页面输出不换行的解决方法
- <textarea>里的内容保存MYSQL后,页面输出不换行的解决方法
- MySQL中使用like查找汉字 Incorrect string value 解决办法
- mysql插入中文出现错误:incorrect string value:'\xD5\xC5\xC8\xFD\xB7\xE1'的解决办法
- java 向 mysql 插入汉字 Incorrect string value 解决办法
- “warning C4251::CStringT<BaseType,StringTraits>需要有 dll 接口”解决办法
- "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const"问题的解决办法
- web.xml不识别<taglib>解决办法
- List<Map<String, Object>> 转换成JSON数据格式保存再转List<Map<String, Object>> 返回
- 点击<a>标签,禁止页面自动跳到顶部的解决办法
- 高德地图#import <MAMapKit/MAMapKit.h> file not found的解决办法
- java.net.BindException: Address already in use: JVM_Bind <null>:8001解决办法