Home > Blockchain >  go-mysql-driver insert string into table gives error 1336 even using utf8mb4
go-mysql-driver insert string into table gives error 1336 even using utf8mb4

Time:09-12

Error message:

Error 1366: Incorrect string value: '\xA7test for column `mycolumn` at row 1

db connection code:

db, err := sql.Open("mysql", "username:pass@tcp(127.0.0.1:3306)/mydb?charset=utf8mb4")

I tried running

SELECT
    `tables`.`TABLE_NAME`,
    `collations`.`character_set_name`
FROM
    `information_schema`.`TABLES` AS `tables`,
    `information_schema`.`COLLATION_CHARACTER_SET_APPLICABILITY` AS `collations`
WHERE
        `tables`.`table_schema` = DATABASE()
  AND `collations`.`collation_name` = `tables`.`table_collation`
;

and got this:

---------------------
| mytable | utf8mb4 |
---------------------

The string I'm trying to insert: §test

I looked at MariaDB incorrect string value error with utf8mb4 encoding and other things, but everything I saw says to set collation to utf8mb4 and to set charset to utf8mb4 which I have done.

CodePudding user response:

The value should be \xC2\xA7test. \xA7 doesn't have a valid mapping in utf8(mb4).

select hex('§test')

C2A774657374

ref: fiddle

  • Related