英文:
sql: Scan error on column index 38: destination not a pointer
问题
使用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
相同。这是为了处理空值情况,对于大多数列似乎都有效,但我不确定为什么它会说“destination not a pointer”。而且,我不知道哪个变量是问题所在。有什么想法吗?
英文:
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
得分: 16
你的最后一个参数longitude没有通过地址传递。
改为 &longitude
另外,使用"select *"并假设列的位置是可预测的,这样做可能会导致未来出现错误。
为了防止未来表格更改导致列顺序问题,你应该命名你选择的所有列。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论