我有以下 sql 语句:
SELECT pk, up FROM mytable WHERE 2 > 1 LIMIT 10
显然,这只是为了简单起见。我能够参数化任何整数:
SELECT pk, up FROM mytable WHERE 2 > $1 LIMIT 10
但是,当我尝试参数化运算符时,例如:
SELECT pk, up FROM mytable WHERE 2 $1 1 LIMIT 10
我得到:
pq: syntax error at or near "$1"
完整代码:
package main
import (
"database/sql"
_ "github.com/lib/pq"
"log"
)
func main() {
log.SetFlags(log.Lshortfile)
Db, err := sql.Open("postgres", "user=yoitsmeletmein password=supersecretyo host=what.a.host dbname=mydb sslmode=require")
if err != nil {
log.Fatal("Cannot connect to db: ", err)
}
q := `SELECT pk FROM mytable WHERE 2 $1 1 LIMIT 10`
params := []interface{}{">"}
rows, err := Db.Query(q, params...)
if err != nil {
log.Println(err)
} else {
defer rows.Close()
for rows.Next() {
var pk int64
if err := rows.Scan(&pk); err != nil {
log.Fatal(err)
}
log.Println(pk)
}
}
}
德玛西亚99
相关分类