donglulong0877 2019-06-16 19:33
浏览 63
已采纳

恐慌:sql:预期0参数,得到1

I am trying to execute queries with Go, but I cannot manage to request any query, because it keeps giving me the same error over and over.

I have changed the query multiple times, but that doesn't seem to help. Also i have changed QueryRow in Query, unfortunately that doesn't help either.

func test123() {

    db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/vitaintellectdb")
    if err != nil {
        panic(err.Error())
    }

    id := 1
    var col string
    sqlStatement := `SELECT naam FROM medewerker WHERE naam="jansen"`
    row := db.QueryRow(sqlStatement, id)
    err2 := row.Scan(&col)
    if err2 != nil {
        if err2 == sql.ErrNoRows {
            fmt.Println("Zero rows found")
        } else {
            panic(err2)
        }
    }

}

QueryRow is designed to give you 1 row in return. Unfortunately the error is telling me that there should be no returns, I expect 1 row in return.

  • 写回答

2条回答 默认 最新

  • doyhq66282 2019-06-16 19:37
    关注
    sqlStatement := `SELECT naam FROM medewerker WHERE naam="jansen"`
    row := db.QueryRow(sqlStatement, id)
    

    The given sql statement has no parameter. The following sql statement including the placeholder might work better.

    sqlStatement := `SELECT naam FROM medewerker WHERE medewerkernummer =?`
    row := db.QueryRow(sqlStatement, id)
    

    Depending how your id-column is named you might have to change naam=? to idColumn=?. In the examples here you might find inspiration.

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

报告相同问题?

悬赏问题

  • ¥15 UE5样条线生成的网格UV问题
  • ¥15 如何用最短的时间大致看懂springboot+vue的项目
  • ¥15 (有偿)懂数值分析和含时变参数微分方程的来
  • ¥15 layui父页的数据表格如何用弹窗页提交后的查询数据来更新数据表格内容?
  • ¥15 abaqus随机生成二维颗粒
  • ¥15 安装ansys许可证管理器时出现了这个问题,如何解决?
  • ¥100 高价求算法,利用智能手机传感器计算车辆的三轴g值
  • ¥15 Blazor server 数据库操作异常,如何解决?(语言-c#)
  • ¥15 uni-app开发APP运行到浏览器访问接口跨域
  • ¥100 mfc消息自创建控件