postgres 中 "$1" 处或附近的 golang 语法错误

我正在尝试使用 sql 模块在 go 中执行查询。


var from string = "2015-03-01 00:00:00"    

rows, err := db.Query("select time, val from table where " +

                              "time >= extract(epoch from timestamp with time zone $1)::int4 " +

                              "and time < extract(epoch from timestamp with time zone '2015-03-01 00:15:10')::int4 " +

                              "order by time asc",from)

但是我得到了错误


pq: syntax error at or near "$1"

如果我直接输入纪元值,那么查询将起作用,并且当我在没有任何变量的情况下尝试查询时,查询将起作用,即使用硬编码的查询。那么问题是什么?


米脂
浏览 458回答 1
1回答

九州编程

你是对的$1和?。它抱怨无效语法的原因$1是因为类型转换。把它像这样:rows, err := db.Query("select time, val from table where " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "time >= extract(epoch from $1::timestamp with time zone)::int4 " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "and time < extract(epoch from timestamp with time zone '2015-03-01 00:15:10')::int4 " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "order by time asc",from)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go