douchen1924 2016-07-03 18:45
浏览 33

具有浮点值的Golang查询无法正常工作

I am doing a Golang query to the postgres database and the weird thing is that the query only works if I hard-code the value in for instance this query works

db.QueryRow("select json_build_object('Streams', array_to_json(array_agg(t))) from (select p.name FROM profiles as p INNER JOIN streams as s ON(s.profile_id = 
p.id) WHERE s.latitudes >=28.1036 AND shared = false order by id desc limit 15)t").Scan(&result)

The only part that I now change is where the WHERE s.latitudes >=28.1036 instead of having that value hard-coded I Past it through a form and now have the query like this

   db.QueryRow("select json_build_object('Streams', array_to_json(array_agg(t))) from (select p.name FROM profiles as p INNER JOIN streams as s ON(s.profile_id = 
    p.id) WHERE s.latitudes>=$1 AND shared = false order by id desc limit 15)t",LatMin).Scan(&result)

Now the query just comes back null and I do know for a fact that the LatMin variable is being populated correctly as this is my code

    func Auto_Location(w http.ResponseWriter, r *http.Request) {

        var result string
        if r.Method == "GET" {

        } else {
            r.ParseForm() }

        LatMin := r.Form["LatMin"]

    db,err := sql.Open("Postgres Connects")
    if err != nil {
    log.Fatal(err)
    println(err)

    }
   db.QueryRow("select json_build_object('Streams', array_to_json(array_agg(t))) from (select p.name FROM profiles as p INNER JOIN streams as s ON(s.profile_id = 
        p.id) WHERE s.latitudes>=$1 AND shared = false order by id desc limit 15)t",LatMin).Scan(&result)
    defer db.Close()


    w.Header().Set("Content-Type", "application/json")


    fmt.Fprintf(w,result)
        fmt.Println("Value:", LatMin)

    }

Again as you can see from the code I am using FMT and the LatMin has the correct value of 28.1036 is there something that I am missing here.. The postgres package I am using is _ "github.com/lib/pq" . I am thinking it is an issue with float values because if I change LatMin to 28 it works but 28.1036 does not

  • 写回答

1条回答 默认 最新

  • doujia3441 2016-07-03 20:17
    关注

    Hello everyone I have found the answer, if you are collecting form information on a Post and if some of the values are of type decimal,float or double and you want to query that value into the database then use

    r.FormValue("LatMin")
    

    instead of

    r.Form["LatMin"]
    

    as that will get the correcte

    Both of those methods get the correct value however Form[""] as issues with the database.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改