Golang中的Postgres查询

时间:2019-09-12 12:33:38

标签: postgresql go

我不确定如何在2dslice中进行postgres查询(col1,col2)的地方

我尝试了以下方法:

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,)

您能建议如何做吗?

1 个答案:

答案 0 :(得分:0)

将光标放在切片上并进行查询

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)
}