go - sql : Scan error on column index 38: destination not a pointer

标签 go

使用 Golang 和内置的 database/sql 库和 postgres lib/pq 库,我试图从一个数据库中读取一些空值一些记录。代码可以编译,但是当我尝试运行它时出现以下错误。

sql: Scan error on column index 38: destination not a pointer

这是我的代码:

  rows, err := db.Query(`SELECT * FROM observations WHERE profile_id=$1 AND year=$2 AND month=$3`, id, date.Year(), int(date.Month()))
  if err != nil {
    log.Fatal(err)
  }
  defer rows.Close()
  for rows.Next() {
    var id sql.NullInt64
    var year sql.NullInt64
    var month sql.NullInt64
    var day_of_week sql.NullInt64 
    var hour sql.NullInt64
    var profile_id sql.NullInt64 
    var created_at time.Time 
    var updated_at time.Time
    var banking sql.NullFloat64
    var hlt_pro sql.NullFloat64 
    var sup_shp sql.NullFloat64 
    var aff_con sql.NullFloat64 
    var biz_trv sql.NullFloat64 
    var investing sql.NullFloat64 
    var day_com sql.NullFloat64 
    var unique_emin sql.NullFloat64 
    var no_group sql.NullFloat64 
    var movie_goer sql.NullFloat64 
    var luxury_shopper sql.NullFloat64 
    var sports_fan sql.NullFloat64 
    var aland sql.NullInt64
    var awater sql.NullInt64
    var tractce sql.NullString
    var geoid sql.NullString
    var median_income sql.NullInt64 
    var total_population sql.NullInt64 
    var white sql.NullInt64 
    var black sql.NullInt64 
    var native_american sql.NullInt64 
    var asian sql.NullInt64 
    var pacific_islander sql.NullInt64 
    var other sql.NullInt64 
    var two_plus sql.NullInt64 
    var two_plus_question sql.NullInt64 
    var confused sql.NullInt64 
    var median_age sql.NullFloat64
    var count sql.NullFloat64
    var latitude sql.NullString
    var longitude sql.NullString
    if err := rows.Scan(&id, &year, &month, &day_of_week, &hour, &profile_id, &created_at, &updated_at, &banking, &hlt_pro, &sup_shp, &aff_con, &biz_trv, &investing, &day_com, &unique_emin, &no_group, &movie_goer, &luxury_shopper, &sports_fan, &aland, &awater, &tractce, &geoid, &median_income, &total_population, &white, &black, &native_american, &asian, &pacific_islander, &other, &two_plus, &two_plus_question, &confused, &median_age, &count, &latitude, longitude); err != nil {
      log.Fatal(err)
    }
    fmt.Println(id, year, month, day_of_week, hour, profile_id, created_at, updated_at, banking, hlt_pro, sup_shp, aff_con, biz_trv, investing, day_com, unique_emin, no_group, movie_goer, luxury_shopper, sports_fan, aland, awater, tractce, geoid, median_income, total_population, white, black, native_american, asian, pacific_islander, other, two_plus, two_plus_question, confused, median_age, count, latitude, longitude)

  }
  if err := rows.Err(); err != nil {
    log.Fatal(err)
  }

我正在使用 sql.NullType,这与使用 *type 是一样的。这是处理空情况的努力,它似乎适用于大多数专栏,但我不确定为什么它说“目的地不是指针”。另外,我不知道哪个变量是问题所在。有什么想法吗?

最佳答案

你的最后一个参数,经度,不是通过地址传递的。

更改为&经度

作为旁注,使用“select *”并假设列位置是可预测的是导致错误的一个秘诀。

您可能应该命名您选择的所有列,以防将来的表更改导致列排序问题。

关于go - sql : Scan error on column index 38: destination not a pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710692/

相关文章:

go - 如何在 gonum 中将矩阵与向量相乘?

ssl - 在 GoLang 中执行 ReverseProxy 时确认 TLS 证书

go - GRPC 消息结构

validation - 我可以为结构中的字符串字段设置默认最大长度吗?

go - exec 没有从 Golang 应用程序运行命令

pointers - 指向指针的指针有什么用?

postgresql - []使用Gorm和Postgres转换为jsonb的字符串

dictionary - range 或 map 返回什么?

go - 对于主题的某些分区,kafka 偏移量和滞后是未知的

csv - 处理大型 csv 文件并限制 goroutines