大家好,今天本人给大家带来文章《Golang Postgres pq 无法扫描到 *string》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容我正在尝试将 postgresql 列表扫描到空字符串片段中。但是,我收到以下错误:
创建教育失败:sql:列索引 14 上扫描错误,名称“描述”:不支持扫描,将 driver.value 类型字符串存储到类型 *[]*string
中。看起来我需要以某种方式自定义扫描仪,但是如何使用 squirrel 来做到这一点?谢谢。
以下是我构建查询的方式:
squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).RunWith(db).Insert("educations").
Columns("id", "school", "city", "state", "degree", "month_start", "year_start", "month_end", "year_end", "\"order\"", "logo_url", "created_at", "updated_at", "style", "descriptions").
Values(
uuid.Must(uuid.NewV4()).String(),
education.School,
education.City,
education.State,
education.Degree,
education.MonthStart,
education.YearStart,
education.MonthEnd,
education.YearEnd,
education.Order,
education.LogoURL,
currentTime,
currentTime,
savedStyle.ID,
pq.Array(education.Descriptions),
).
Suffix("RETURNING *").
Scan(
&savedEducation.ID,
&savedEducation.School,
&savedEducation.City,
&savedEducation.State,
&savedEducation.Degree,
&savedEducation.MonthStart,
&savedEducation.YearStart,
&savedEducation.MonthEnd,
&savedEducation.YearEnd,
&savedEducation.Order,
&savedEducation.LogoURL,
&savedEducation.CreatedAt,
&savedEducation.UpdatedAt,
&ignored,
&savedEducation.Descriptions,
)
解决方案
您需要对字段 savedEducation.Descriptions
使用 pq.StringArray
类型。这是一个 []string
,带有 pq 库提供的附加 Value()
和 Scan()
方法。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持编程网!更多关于Golang的相关知识,也可关注编程网公众号。