我正在尝试使用sql模块执行查询。
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"
如果我直接输入纪元值,那么查询将起作用,当我尝试没有任何变量时查询工作,即查询硬编码。 那么问题是什么?
答案 0 :(得分:6)
你是$1
和?
。
它抱怨$1
语法无效的原因是因为类型转换。这样说:
rows, err := db.Query("select time, val from table where " +
"time >= extract(epoch from $1::timestamp with time zone)::int4 " +
"and time < extract(epoch from timestamp with time zone '2015-03-01 00:15:10')::int4 " +
"order by time asc",from)