doudizhu2222 2015-07-29 20:42
浏览 255
已采纳

sql:列索引38上的扫描错误:目标不是指针

Using Golang and the built in database/sql library and the postgres lib/pq library, I'm trying to read from a database that has some null values in some of the records. the code compiles, but when I try to run it I get the following error.

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

Here is my code:

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

I'm using sql.NullType, which is the same as doing *type. This is an effort handle the null situation, and it seems to be working for most columns, but I'm not sure why it's saying "destination not a pointer". Also, I have no idea which variable is the problem. Any ideas?

  • 写回答

1条回答 默认 最新

  • doufu1950 2015-07-29 21:03
    关注

    Your last parameter, longitude, is not being passed by address.

    change to &longitude

    as a side note, using "select *" and assuming column position is predictable is a recipe for down the road errors.

    You probably should name all the columns you select, in case a future table change causes a column ordering issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?