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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?