doude1917 2018-01-15 05:47
浏览 42

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

I understand what the issue is, I am not providing a pointer address when I am trying to scan the fields from my database call - however in this situation I am not quite sure how to work around it.

I have a struct that looks like this:

type ItemsRequest struct {
    Sku      string `json:"sku"`
    Name     string `json:"name"`
    Barcode  string `json:"barcode,omitempty"`
    Category struct {
        ID               string        `json:"id,omitempty"`
        Name             string        `json:"name,omitempty"`
        Subcategories    []interface{} `json:"subcategories,omitempty"`
        CategoryPosition int           `json:"categoryPosition,omitempty"`
        ResourceURI      string        `json:"resource_uri,omitempty"`
    } `json:"category,omitempty"`
    CategoryPosition int               `json:"categoryPosition,omitempty"`
    LongDesc         string            `json:"longDesc,omitempty"`
    MinQty           int               `json:"minQty,omitempty"`
    MultQty          int               `json:"multQty,omitempty"`
    UnitPrice        string            `json:"unitPrice,omitempty"`
    ImageURLs        []string          `json:"imageURLs,omitempty"`
    Variants         []interface{}     `json:"variants,omitempty"`
    AdditionalPrices map[string]string `json:"additionalPrices,omitempty"`
    audtdatetime     int
}

The AdditionalPrices field is meant to represent an object of values, e.g:

{
    'UK': "12.34",
    'AU': "14.00"
}

As such, I'm fairly confident that the map[string]string type for the field is correct...

I am retrieving data to populate this struct from my database and then trying to scan the results into an instance of this struct. In my query, I have a predefined number of columns for which I want to populate AdditionalPrices with.

As such I started off with this:

func getItemsFromDB() []*ItemsRequest {
    rows, err := bcdataDBConnection.Query(`
        SELECT  *
        FROM    vProducts
        WHERE   max_audit_date_time > @AuditTime
    `, sql.Named("AUDITTIME", 2016101412000000))

    if err != nil {
        log.Fatal("There was an issue with the query for items: ", err)
    }

    defer rows.Close()

    var items []*ItemsRequest

    for rows.Next() {
        var item = &ItemsRequest{}
        err := rows.Scan(
            &item.Sku,
            &item.Name,
            &item.Category.ID,
            &item.Category.Name,
            &item.Category.CategoryPosition,
            &item.LongDesc,
            &item.Barcode,
            &item.UnitPrice,
            &item.MinQty,
            &item.MultQty,
            &item.CategoryPosition,
            &item.AdditionalPrices["NZBASE"],
            &item.AdditionalPrices["NZWWW"],
            &item.AdditionalPrices["MMCMFR"],
            &item.AdditionalPrices["MMBASE"],
            &item.AdditionalPrices["AUBASE"],
            &item.AdditionalPrices["MMCMAU"],
            &item.AdditionalPrices["AUDSA"],
            &item.AdditionalPrices["AUDVIC"],
            &item.AdditionalPrices["AUDWWW"],
            &item.audtdatetime,
        )
        if err != nil {
            log.Fatal("Scan error: ", err)
        }

        items = append(items, item)
    }

    return items
}

However this didn't appear to be correct syntax and so I changed it to:

func getItemsFromDB() []*ItemsRequest {
    rows, err := bcdataDBConnection.Query(`
        SELECT  *
        FROM    vHandshakeProducts
        WHERE   max_audit_date_time > @AuditTime
    `, sql.Named("AUDITTIME", 2016101412000000))

    if err != nil {
        log.Fatal("There was an issue with the query for items: ", err)
    }

    defer rows.Close()

    var items []*ItemsRequest

    for rows.Next() {
        var item = &ItemsRequest{}
        err := rows.Scan(
            &item.Sku,
            &item.Name,
            &item.Category.ID,
            &item.Category.Name,
            &item.Category.CategoryPosition,
            &item.LongDesc,
            &item.Barcode,
            &item.UnitPrice,
            &item.MinQty,
            &item.MultQty,
            &item.CategoryPosition,
            item.AdditionalPrices["NZBASE"],
            item.AdditionalPrices["NZWWW"],
            item.AdditionalPrices["MMCMFR"],
            item.AdditionalPrices["MMBASE"],
            item.AdditionalPrices["AUBASE"],
            item.AdditionalPrices["MMCMAU"],
            item.AdditionalPrices["AUDSA"],
            item.AdditionalPrices["AUDVIC"],
            item.AdditionalPrices["AUDWWW"],
            &item.audtdatetime,
        )
        if err != nil {
            log.Fatal("Scan error: ", err)
        }

        items = append(items, item)
    }

    return items
}

Which my IDE likes more, however upon running it I get the error:

2018/01/15 15:35:06 Scan error: sql: Scan error on column index 11: destination not a pointer

So at this point, I'm not quite sure how to populate the values for AdditionalPrices from my database call.

What do I need to do to populate the AdditionalPrices map with the values I get from the database? As per the keys in my code, I already know the map keys for which I would like to assign the values to.

  • 写回答

1条回答 默认 最新

  • douyu3145 2018-01-15 06:31
    关注

    The row.Scan is meant to scan the fields in a DB row. It cannot scan the values of the field in a nested way.

    So just say &item.AdditionalPrices to scan the additionalPrices field of each row, as follows:

        err := rows.Scan(
            &item.Sku,
            &item.Name,
            &item.Category.ID,
            &item.Category.Name,
            &item.Category.CategoryPosition,
            &item.LongDesc,
            &item.Barcode,
            &item.UnitPrice,
            &item.MinQty,
            &item.MultQty,
            &item.CategoryPosition,
            &item.AdditionalPrices,
            &item.audtdatetime,
        )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题