golang语法错误在或附近" $ 1"在postgres

时间:2015-03-12 11:01:03

标签: postgresql go

我正在尝试使用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"

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

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)