duan0714 2015-09-01 04:57
浏览 28
已采纳

在Golang中处理西班牙波浪

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 |
  • 写回答

2条回答 默认 最新

  • dtpngq3378499 2015-09-01 05:20
    关注

    Please change collation to utf8_general_ci. it should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanjizhan9353 2015-09-01 05:14
    关注

    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.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
  • ¥15 蓝桥杯c51单片机问题
  • ¥15 ajax跨域问题请求修改代码
  • ¥15 python matplotlib
  • ¥15 短信测压+语音,有偿,必须用Python
  • ¥20 COCOS2DX的protobuf协议注册函数问题
  • ¥15 (标签-Pytorch|关键词-Stream)
  • ¥15 求深圳2019年开放数据应用创新大赛的营运车辆数据!
  • ¥15 软件UI界面绘制折线图
  • ¥20 用c语言写一个团队考勤系统