处理Golang中的西班牙语重音符号

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

Dealing with spanish tilde in Golang

问题

所以我正在尝试使用SQL驱动程序将这个字符串输入到MySQL数据库中。我遇到了这个错误 -

Do?a Merced Elementary
panic: Error 1366: Incorrect string value: '\x96a Mer...' for column 'name' at row 1

我考虑过排除这个条目,但是还没有成功。我尝试过以下方法 -

if !strings.ContainsAny(splitStr[2], "U+0303") {
if !strings.ContainsAny(splitStr[2], '\x96') {

但是都没有起作用。

最好让MySQL处理这个问题,但我不确定如何做。

有什么建议吗?

编辑

这是我连接到数据库的方式

db, err := sql.Open("mysql", "psanker:123@/education_data")
err = db.Ping()

db.SetMaxOpenConns(0)
check(err)
if err != nil {
    fmt.Println("Failed to prepare connection to database")
    log.Fatal("Error:", err.Error())
}

这是出现问题的地方

districtResult, err := db.Exec("INSERT INTO districts(name) VALUES(?)", strings.TrimSpace(splitStr[2]))
check(err)

SHOW CREATE TABLE的输出

---------------------------------+
| Table     | Create Table                                                                                                                                                                                                                   |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| districts | CREATE TABLE `districts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
英文:

So I'm trying to enter this string into a mysql database using a sql driver. I get this error -

Do?a Merced Elementary
panic: Error 1366: Incorrect string value: '\x96a Mer...' for column 'name' at row 1

I thought about excluding this entry, but haven't been able to do so. I've tried -

if !strings.ContainsAny(splitStr[2], "U+0303") {
if !strings.ContainsAny(splitStr[2], '\x96') {

but that has not worked.

It would be better to have mysql deal with this, but I'm not sure how.

Any suggestions?

EDIT

This is how I connect to my db

db, err := sql.Open("mysql", "psanker:123@/education_data")
err = db.Ping()

db.SetMaxOpenConns(0)
check(err)
if err != nil {
	fmt.Println("Failed to prepare connection to database")
	log.Fatal("Error:", err.Error())
}

This is where my issue comes

districtResult, err := db.Exec("INSERT INTO districts(name) VALUES(?)", strings.TrimSpace(splitStr[2]))
check(err)

Output of SHOW CREATE TABLE

---------------------------------+
| Table     | Create Table                                                                                                                                                                                                                   |
+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| districts | CREATE TABLE `districts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

答案1

得分: 0

你是在使用苹果电脑吗?我猜你原本想说的是Doña Merced Elementary吧?但是,要从x96得到ñ,你必须先使用"MacRoman"编码的文本。

一个简单的解决方案可能是在连接到MySQL后立即执行SET NAMES macroman命令。

英文:

Running on an Apple? I assume that should have said Doña Merced Elementary? Yet, to get ñ from x96, you must have started with text in the "MacRoman" encoding.

The simple solution might be to do SET NAMES macroman right after connecting to MySQL.

答案2

得分: 0

请将排序规则更改为utf8_general_ci。应该可以正常工作。

英文:

Please change collation to utf8_general_ci. it should work

huangapple
  • 本文由 发表于 2015年9月1日 12:57:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/32323816.html
匿名

发表评论

匿名网友

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

确定