dongwan0574 2016-03-18 11:25
浏览 42
已采纳

不支持的扫描:SQL UUID数组

I am trying to get an array of UUID from a PostgreSQL DB, this gives the following error:

sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *[]string

Fetching a single UUID is no problem, but when it is an array, the Scan function infers the type of the elements as uint8.

Is there a workaround / solution for this? Or should I rethink my DB?

Code :

func FetchListIdsForUser(id string, db *sql.DB) ([]string, error) {

        // where the results will be stored

        var (
            lists []string
        )


        // statement prep

        stmt, err := db.Prepare("select lists from users where object_id = $1")
            if err != nil {
            formattedError := er.New("FetchListIdsForUser SQL Select: " + err.Error())
            log.Println(formattedError)
            return nil,formattedError
        }
        defer stmt.Close()


        // query happening

        rows, err := stmt.Query(id)
        if err != nil {
            formattedError := er.New("FetchListIdsForUser SQL Query: " + err.Error())
            log.Println(formattedError)
            return nil,formattedError
        }

        defer rows.Close()


        // for each row

        for rows.Next() {

            // scan : this is where the error happens.

            err := rows.Scan(&lists)
            if err != nil {
                formattedError := er.New("FetchListIdsForUser SQL Scan: " + err.Error())
                log.Println(formattedError)
                return nil,formattedError
            }
            return lists,nil
        }

        err = rows.Err()
        if err != nil {
            formattedError := er.New("FetchListIdsForUser: " + id + " Does Not Exist")
            log.Println(formattedError)
            return nil,formattedError
        }
        return nil,er.New("FetchListIdsForUser: " + id + " Does Not Exist")
}
  • 写回答

1条回答 默认 最新

  • ds211107 2016-03-18 13:44
    关注

    Your code can be simplified a lot:

    func FetchListIdsForUser(id string, db *sql.DB) ([]string, error) {
    
            rows, err := db.Query("SELECT unnest(lists) FROM users WHERE object_id = $1", id)
            if err != nil { 
                formattedError := errors.New("FetchListIdsForUser SQL Query: " + err.Error())
                return nil, formattedError
            }
    
            defer rows.Close()
    
            var lists []string
            var list string
    
            for rows.Next() {
                // scan every item of the array and append it to lists
                err := rows.Scan(&list)
                if err != nil {
                    formattedError := errors.New("FetchListIdsForUser SQL Scan: " + err.Error())
                    return nil,formattedError
                }
                lists = append(lists, list)
            }
    
            if lists == nil {
                // no rows returned
                formattedError := errors.New("FetchListIdsForUser: " + id + " Does Not Exist")
                return nil, formattedError
            }
    
            return lists, nil
    }
    
    • db.Query() will prepare (and cache) the statement for you. There's no need to do that manually.
    • I added unnest(lists) to the select statement. This will produce one row for every element in the array.
    • In the for loop, we scan every element individually and append it to lists.

    Also, this driver supports scanning arrays out of the box.

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

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?