“运算符不存在:整数 =?"使用 Postgres 时 [英] "Operator does not exist: integer =?" when using Postgres

查看:27
本文介绍了“运算符不存在:整数 =?"使用 Postgres 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 go 的 database/sql 包提供的 QueryRow 方法中调用了一个简单的 SQL 查询.

I have a simple SQL query called within the QueryRow method provided by go's database/sql package.

import (
  "github.com/codegangsta/martini"
  "github.com/martini-contrib/render"
  "net/http"
  "database/sql"
  "fmt"
  _ "github.com/lib/pq")
)

type User struct {
  Name string
}

func Show(db *sql.DB, params martini.Params) {
  id := params["id"]
  row := db.QueryRow(
    "SELECT name FROM users WHERE id=?", id)
  u := User{}
  err := row.Scan(&u.Name)
  fmt.Println(err)
}

但是,我收到错误 pq: operator does not exist: integer =? 看起来代码不明白 ? 只是一个占位符.我该如何解决这个问题?

However, I'm getting the error pq: operator does not exist: integer =? It looks like the code doesn't understand that the ? is just a placeholder. How can I fix this?

推荐答案

PostgreSQL 使用编号占位符($1, $2, ...)位置问号.Go 接口文档也在其示例中使用了编号占位符:

PostgreSQL works with numbered placeholders ($1, $2, ...) natively rather than the usual positional question marks. The documentation for the Go interface also uses numbered placeholders in its examples:

rows, err := db.Query("SELECT name FROM users WHERE age = $1", age)

似乎 Go 接口并没有像许多接口那样将问号转换为编号的占位符,因此问号一直进入数据库并混淆了一切.

Seems that the Go interface isn't translating the question marks to numbered placeholders the way many interfaces do so the question mark is getting all the way to the database and confusing everything.

您应该能够切换到编号占位符而不是问号:

You should be able to switch to numbered placeholders instead of question marks:

 row := db.QueryRow(
    "SELECT name FROM users WHERE id = $1", id)

这篇关于“运算符不存在:整数 =?"使用 Postgres 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆