当使用Go驱动程序插入时,MySQL出现编码问题

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

MySQL encoding problem when inserting with Go driver

问题

我正在尝试将UTF-8文本存储到一个编码为latin1_swedish_ci的表中。由于我无法直接访问数据库,所以无法更改编码方式。因此,我尝试使用这个Go库对文本进行Latin-1编码,该库提供了一个编码器和一个函数,该函数包装了编码器,以便替换无效字符而不是返回错误。

但是,当我尝试插入行时,MySQL报错Error 1366: Incorrect string value: '\\xE7\\xE3o pa...' for column 'description' at row 1

我尝试将相同的文本写入文件,并且file -I报告如下结果:file.txt: application/octet-stream; charset=binary

示例代码如下:

package main

import (
	"fmt"
	"os"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
)

func main() {
	s := "foo – bar"

	encoder := charmap.ISO8859_1.NewEncoder()
	encoder = encoding.ReplaceUnsupported(encoder)

	encoded, err := encoder.String(s)
	if err != nil {
		panic(err)
	}

	fmt.Println(s)
	fmt.Println(encoded)
	fmt.Printf("%q\n", encoded)

	/* file test */
	f, err := os.Create("file.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	w := encoder.Writer(f)
	w.Write([]byte(s))
}

我可能忽略了一些非常明显的东西,因为我对编码的了解非常有限。

提前感谢您的帮助。

英文:

I'm trying to store utf-8 text into a table which encoding is latin1_swedish_ci. I can't change the encoding since I do not have direct access to the the db. So what I'm trying is encode the text into latin-1 with this Go library that provides the encoder and this one that has a function that wraps the encoder so it replaces the invalid characters instead of returning an error.

But when I try to insert the row mysql complains Error 1366: Incorrect string value: '\\xE7\\xE3o pa...' for column 'description' at row 1.

I tried writing the same text to a file and file -I reports this file.txt: application/octet-stream; charset=binary.

Example

package main

import (
	"fmt"
	"os"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
)

func main() {
	s := "foo – bar"

	encoder := charmap.ISO8859_1.NewEncoder()
	encoder = encoding.ReplaceUnsupported(encoder)

	encoded, err := encoder.String(s)
	if err != nil {
		panic(err)
	}

	fmt.Println(s)
	fmt.Println(encoded)
	fmt.Printf("%q\n", encoded)

	/* file test */
	f, err := os.Create("file.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	w := encoder.Writer(f)
	w.Write([]byte(s))
}
    

I'm probably missing something very obvious but my knowledge about encodings is very poor.

Thanks in advace.

答案1

得分: 1

你是否期望得到 çã

这个问题很容易解决。MySQL在插入文本时可以很容易地从latin1转换为utf8。但是你必须告诉MySQL你的客户端正在使用latin1编码。这可能是在连接到MySQL时完成的,并且可能当前默认为utf8或UTF-8或utf8mb4。类似于以下设置:

charset=latin1
英文:

Were you expecting çã ?

The problem is easily solved. MySQL will gladly translate from latin1 to utf8 while INSERTing text. But you must tell it that your client is using latin1. That is probably done during the connection to MySQL, and is probably defaulted to utf8 or UTF-8 or utf8mb4 currently. It is something like

charset=latin1

huangapple
  • 本文由 发表于 2023年1月24日 06:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75215657.html
匿名

发表评论

匿名网友

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

确定