go-mysql-driver将字符串插入表中时出现错误1336,即使使用utf8mb4也是如此。

huangapple go评论78阅读模式
英文:

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

问题

错误信息:

错误 1366: 第1行的列 `mycolumn` 的字符串值不正确:'\xA7test

数据库连接代码:

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

我尝试运行以下代码:

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`
;

并得到以下结果:

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

我尝试插入的字符串是:§test

我查看了https://stackoverflow.com/questions/56700901/mariadb-incorrect-string-value-error-with-utf8mb4-encoding和其他一些内容,但我看到的所有内容都说要将排序规则设置为utf8mb4,并将字符集设置为utf8mb4,我已经这样做了。

英文:

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 https://stackoverflow.com/questions/56700901/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.

答案1

得分: 0

这个值应该是\xC2\xA7test\xA7在utf8(mb4)中没有有效的映射。

select hex('§test')

C2A774657374

参考:fiddle

英文:

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

select hex('§test')

C2A774657374

ref: fiddle

huangapple
  • 本文由 发表于 2022年9月12日 07:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73683361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定