dongzhuo1498 2019-09-12 12:33
浏览 349

Golang中的Postgres查询

I am not sure how to make postgres query where (col1, col2) in 2dslice

I have tried the following:

CREATE TABLE table2 (
  id CHAR(27) NOT NULL,
  lat FLOAT8 NOT NULL,
  lon FLOAT8 NOT NULL,
  PRIMARY KEY (id)
);


latlongdata := [][]float64{}
latlongdata = append(latlongdata, []float64{1.2, 2.3},)
latlongdata = append(latlongdata, []float64{1.3, 2.4},)
......................................
latlongdata = append(latlongdata, []float64{1.4, 2.5},)

fmt.Println(latlongdata)// prints [[1.2 2.3] [1.3 2.4] ....... [1.4 2.5]] (very long array)

Query: r.db.QueryContext(ctx,("SELECT id, lat, lon FROM table2 WHERE (lat, lon) IN $1", latlongdata,)

Can you please suggest how to do it?

  • 写回答

1条回答 默认 最新

  • dongluxin6711 2019-09-12 12:49
    关注

    Range over the slice and make the query

    for _, data := range latlongdata {
     lat := data[0]
     long := data[1]
    
     err:= r.db.QueryContext(ctx,("SELECT id, lat, lon FROM table2 WHERE lat=$1 AND lon=$2", lat, lon,).scan(&variable)
    
      if err != nil {
        log.Fatal("unable to execute search query", err)
      }
      // also check if all the fields of "variable" are not null
      // then append the result to an array of variable
      variables = append(variables, variable)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型