doulai2573 2018-10-25 07:44
浏览 81
已采纳

SQL选择查询的循环结果

So am learning Go - Language atm and I still confused on how do I iterate arraylist that I get from SQL when I execute my query

here is for the detail

I got a file named CustomerDao.go which contains a bunch of queries and the query I uses now is

SELECT mst_customer.mcus_mlok_pk , mst_customer.mcus_mlok_kode , mst_customer.mcus_nama FROM frontend.mst_customer;

which will return 50 rows of data and 3 columns yes? now the confusing part is when I reach my controller which is like this

func GetArrayListCustomer(queryType string) (map[string]CustomerModelResponse, error) {
    logger := customlogger.GetInstance()
    logger.Println("Service: GetArrayListCustomer Start")

    queryText := dao.CustomerDAO(queryType)
    res, err := config.GetDbByPath("myDatabase").GetDb().Query(queryText)

    mapList := make(map[string]CustomerModelResponse)
    var custResponse CustomerModelResponse

    if config.FancyHandleError(err) {
        logger.Println("Service: GetArrayListCustomer -> Error " + err.Error())
        return mapList, err
    }
    defer res.Close()
    for res.Next() {
        errs := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama)
        for _, eachCust := range res {
            //the error goes here , for some reason
        }
    }
    return mapList, nil
}

and then I try to loop the value then the error show that I cannot range over res which is a *sql.Rows how do I declare those result as an array not as a *sql.Rows type?

  • 写回答

2条回答 默认 最新

  • dongwo6477 2018-10-25 14:28
    关注

    You need to scan your values into a struct, using rows.Next you loop over the sql results your query gives, you need to scan those results (on each loop) into your desired struct, and save that struct into something (a map in this case)

    func GetArrayListCustomer(queryType string) (map[string]CustomerModelResponse, error) {
        logger := customlogger.GetInstance()
        logger.Println("Service: GetArrayListCustomer Start")
    
        queryText := dao.CustomerDAO(queryType)
        res, err := config.GetDbByPath("myDatabase").GetDb().Query(queryText)
    
        mapList := make(map[string]CustomerModelResponse)
    
        if config.FancyHandleError(err) {
            logger.Println("Service: GetArrayListCustomer -> Error " + err.Error())
            return mapList, err
        }
        defer res.Close()
        for res.Next() {
            var custResponse CustomerModelResponse
            if err := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama); err != nil {
                // Handle error
            }
            mapList[cutResponse.McusmlokPk] = custResponse
        }
        // Now mapList is a map with McusmlokPk as key and CustomerModelResponse struct as value
        return mapList, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?