在我的代码里有一项写入数据库的操作(使用了 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 并重新运行网站后,这个问题还会出现,重新查看编码的时候发现编码又被改回去了,这是为什么?我需要重建数据库吗,如果需要的话应当怎么做?