Mysql2 ::错误:字符串值不正确 [英] Mysql2::Error: Incorrect string value
问题描述
我有一个运行在生产模式的rails应用程序,但是当用户尝试保存一个记录时,所有的突然出现这个错误。
I have a rails application running on production mode, but all of the sudden this error came up today when a user tried to save a record.
Mysql2::Error: Incorrect string value
更多细节(从生产日志):
More details (from production log):
Parameters: {"utf8"=>"â<9c><93>" ...
Mysql2::Error: Incorrect string value: '\xC5\x99\xC3\xA1k
Mysql2::Error: Incorrect string value: '\xC5\x99\xC3\xA1k
现在我看到一些需要删除的解决方案数据库和重新创建它,但我不能这样做。
Now I saw some solutions that required dropping the databases and recreating it, but I cannot do that.
现在mysql显示这个:
Now mysql shows this:
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.04 sec)
什么是错误的,如何更改它,所以我没有任何问题的任何字符?
What is wrong and how can I change it so I do not have any problems with any characters?
另外:这个问题可以解决javascript?发送之前转换?
Also: Is this problem solvable with javascript? Convert it before sending it ?
感谢
推荐答案
问题是由您的mysql服务器端的字符串引起。您可以手动配置:
the problem is caused by charset of your mysql server side. You can config manually like:
ALTER TABLE your_database_name.your_table CONVERT TO CHARACTER SET utf8
或删除表并重新创建它:
or drop the table and recreate it like:
rake db:drop
rake db:create
rake db:migrate
参考文献:
https://stackoverflow.com/a/18498210/ 2034097
https://stackoverflow.com/a/ 16934647/2034097
更新
第一个命令只影响指定的表,如果要更改数据库中的所有表,可以像
the first command only affect specified table, if you want to change all the tables in a database, you can do like
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
参考:
https://stackoverflow.com/a/6115705/2034097
这篇关于Mysql2 ::错误:字符串值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!