Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and ...

wendzhue
  • 64

在我的代码里有一项写入数据库的操作(使用了 flask-sqlalchemy 和 pymysql)

new_course = Course(course_name=form.course_name.data,
                                course_code=form.course_code.data,
                                file_count=0)

前一个字段可以是汉字,第二个字段必须是字母加数字组合。

然后在网站运行的时候遇到了这个错误:

(1267, u"Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")

按照别人的说法我更改了几个字段的编码

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

mysql> set character_set_database =utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_results =utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_server =utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_system =utf8;
ERROR 1238 (HY000): Variable 'character_set_system' is a read only variable
mysql> SET collation_server = utf8_general_ci;
Query OK, 0 rows affected (0.00 sec)

mysql> SET collation_database = utf8_general_ci;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

然而在我 exit 并重新运行网站后,这个问题还会出现,重新查看编码的时候发现编码又被改回去了,这是为什么?我需要重建数据库吗,如果需要的话应当怎么做?

回复
阅读 3.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏