【问题标题】:sql: Scan error on column index 38: destination not a pointersql:列索引 38 上的扫描错误:目标不是指针
【发布时间】:2015-07-30 05:03:34
【问题描述】:

使用 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 相同。这是处理空情况的努力,它似乎适用于大多数列,但我不确定为什么它说“目标不是指针”。另外,我不知道哪个变量是问题所在。有什么想法吗?

【问题讨论】:

    标签: go


    【解决方案1】:

    您的最后一个参数,经度,没有通过地址传递。

    更改为 &longitude

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

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

    【讨论】:

    • 呃。谢谢!!我花了一个多小时试图弄清楚为什么会出现这个错误,并检查了很多次,但完全错过了。
    • 感谢您的推荐。我会这样做的。
    猜你喜欢
    • 2018-06-23
    • 2021-03-19
    • 2021-08-12
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多