SQL选择查询的循环结果

时间:2018-10-25 07:44:39

标签: arrays go arraylist

我正在学习Go-Language atm 而且我仍然困惑如何执行查询时从SQL获取的数组列表

这里是详细信息

我有一个名为 CustomerDao.go 的文件 其中包含一堆查询,而我现在使用的查询是

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

哪个将返回50行数据和3列,是吗? 现在令人困惑的部分是当我到达控制器时 就像这样

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
}

然后我尝试循环该值,然后错误显示我无法在* sql.Rows的res上进行范围调整。如何将这些结果声明为数组而不是* sql.Rows类型?

2 个答案:

答案 0 :(得分:1)

您不能在for range上使用res循环。 res只能使用Next()进行迭代。您可以执行以下操作:

for res.Next() {

    //CREATE A NEW ZERO VALUE CustomerModelResponse FOR EACH ITERATION
    var custResponse CustomerModelResponse

    // UPDATE CustomerModelResponse WITH ROW DATA
    errs := res.Scan(&custResponse.McusmlokPk, &custResponse.McusmlokKode, &custResponse.McusNama)

    if errs == nil {
        // APPEND THE NEWLY CREATED & UPDATED CustomerModelResponse TO mapList
        // ASSUMINg mst_customer.mcus_mlok_pk AS KEY FOR THE MAP

        mapList[custResponse.McusmlokPk] = custResponse
    }

    /*
        THIS LOOP IS NOT NEEDED ANY MORE

        for _, eachCust := range res {
            //the error goes here , for some reason
        }
    */
}

我希望这会有所帮助。

答案 1 :(得分:1)

您需要将值扫描到结构中,使用rows.Next遍历查询给出的sql结果,需要(在每个循环中)将这些结果扫描到所需的结构中,并将该结构保存到东西(在这种情况下是地图)

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
}