英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论