Character problem with űő

hornyakn2 years ago

I got error if there is ű or ő character in the field: https://www.imgpaste.net/image/KHnsgU

Data truncation: Incorrect string value: '\xC5\x91\xC5\xB1' for column traccar.tc_maintenances.name at row 1 - MysqlDataTruncation (... < QueryBuilder:469 < DatabaseStorage:95 < BaseObjectResource:71 < ...)

Anton Tananaev2 years ago

Have you tried searching? This has been discussed before. If I remember correctly, you have to configure the text encoding in your database.

hornyakn2 years ago

Ahh yeah, I found this: https://www.traccar.org/forums/topic/fixing-encoding-for-mysql/
So I do the following:

  1. Stop traccar server.
  2. login from mysql cli as root: mysql -B -u root -p traccar
  3. Run the following script (I found it here: https://stackoverflow.com/questions/10859966/how-to-convert-all-tables-in-database-to-one-collation#10860122)
-- set your table name here
SET @MY_SCHEMA = "traccar";

-- tables
SELECT DISTINCT
    CONCAT("ALTER TABLE ", TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as queries
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA=@MY_SCHEMA
  AND TABLE_TYPE="BASE TABLE"

UNION

-- table columns
SELECT DISTINCT
    CONCAT("ALTER TABLE ", C.TABLE_NAME, " CHANGE ", C.COLUMN_NAME, " ", C.COLUMN_NAME, " ", C.COLUMN_TYPE, " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") as queries
FROM INFORMATION_SCHEMA.COLUMNS as C
    LEFT JOIN INFORMATION_SCHEMA.TABLES as T
        ON C.TABLE_NAME = T.TABLE_NAME
WHERE C.COLLATION_NAME is not null
    AND C.TABLE_SCHEMA=@MY_SCHEMA
    AND T.TABLE_TYPE="BASE TABLE"

UNION

-- views
SELECT DISTINCT
    CONCAT("CREATE OR REPLACE VIEW ", V.TABLE_NAME, " AS ", V.VIEW_DEFINITION, ";") as queries
FROM INFORMATION_SCHEMA.VIEWS as V
    LEFT JOIN INFORMATION_SCHEMA.TABLES as T
        ON V.TABLE_NAME = T.TABLE_NAME
WHERE V.TABLE_SCHEMA=@MY_SCHEMA
    AND T.TABLE_TYPE="VIEW";

This is generate all necessarry sql command for convert. Copy the output to clipboard or save to a file.
4. Close the connection, reopen without -B: mysql -u root -p traccar (now root is not required)
5. Paste it and run the output of the previous function.
6. Exit from mysql cli and restart traccar server.

Works fine. :)