Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql2::Error: Incorrect string value for Turkish characters

In a Rails 4.0 app with Ruby 2.0, Turkish characters lead to the following ActiveRecord / MySQL error while trying to insert a record in the database. The problematic characters are for instance ğ and ş, but there is no problem with ü or Ç (which also seem to occur in latin charsets).

ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: 
                               '\xC4\x9Fu\xC5\x9F ...' for column ...

How do you prevent this error? The application and the database use UTF-8 as standard encoding. "xC4\x9F" is UTF-8 encoding for "ğ", "\xC5\x9F" is UTF-8 for "ş". Both seem to be the problematic special characters. There is no problem with German (äöü) or French (áàâ) special characters. Contrary to ISO 8859-1 or ISO 8859-15 (only ISO 8859-9 supports all Turkish characters) it should be possible to store Turkish characters in UTF-8.

The MySQL collection settings for the database are as follows. Would it be helpful to switch collection_database to a different value, such as utf8_unicode_ci?

mysql> show variables like '%collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
like image 886
0x4a6f4672 Avatar asked Oct 11 '13 15:10

0x4a6f4672


People also ask

What is incorrect string value?

To conclude, the ERROR 1366: Incorrect string value happens when MySQL can't insert the value you specified into the table because of incompatible encoding. You need to modify or remove characters that have 4-bytes UTF-8 encoding, or you can change the encoding and collation used by MySQL.15-Dec-2021.

What is Error 1366?

ERROR 1366 (22007): Incorrect string value On the old MySQL database, dump the database with the follow options and compress it. old$ mysqldump -u root -p --opt --quote-names --skip-set-charset --default-character-set=latin1 rt34 > /tmp/just-rt34.sql. old$ gzip -9 /tmp/just-rt34.sql.


1 Answers

The reason was apparently that the database (and especially the database tables) had the wrong character set. Switching the collation of the database alone did not help. Inspection of the database tables showed that each table still had the latin1 charset, which can not store all utf8 characters:

mysql> show table status;
+----------+--------+-------------------+ ..
| Name     | Engine | Collation         | ..
+----------+--------+-------------------+ ..
| my_table | InnoDB | latin1_swedish_ci | ..

Therefore I altered the character set of the table directly:

ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;

Then it finally worked, and the character set is how utf8

   mysql> show table status;
    ... +-------------------+ ..
    ... | Collation         | .. 
    ... +-------------------+ ..
    ....| utf8_general_ci   | ..
like image 154
0x4a6f4672 Avatar answered Oct 02 '22 23:10

0x4a6f4672