-2

我正在使用 Go 1.13 Postgres 11 和 GraphQL

我正在尝试运行三个不同的查询: 1. 在某些条件为真时获取少数员工的详细信息。2. 获取所有员工的所有详细信息。3. 在匹配 Empid 时获取员工的所有详细信息。

当我运行我的代码时,它编译成功

但是当我为(2和3)调用API时,它说无法获取。1 的 API 成功运行。

在浏览解决方案并尝试查找错误发生的位置时,它会在 rows.Scan() 函数处收敛。

当我评论 rows.Scan() 行时,它在不提供数据的情况下运行,但在取消注释后运行时

显示错误:获取失败。

所以错误在于 rows.Scan()

也许我们在一个项目中不能有多个 rows.Scan()。 考虑到这一点,我创建了一个接受查询语句并返回接口的函数。

但这也没有奏效。

    connStr := fmt.Sprintf("port=%d host=%s user=%s password=%s dbname=%s sslmode=disable", hostport, hostname, username, password, databaseName)
    db, err := sql.Open("postgres", connStr)

    if err != nil {
        fmt.Println(`Could not connect to db`)
        panic(err)
    }
    defer db.Close()

    rows, err := db.Query(query)
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    for rows.Next() {
        row := models.Universal{}
        if num == 1 {
            if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.PFNo, &row.BloodGroup, &row.PAN, &row.PersonalEmail, &row.FatherName, &row.EmergencyNo, &row.BankAcNo, &row.EmpBank, &row.IFSC, &row.Designation, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        } else if num == 2 {
            if err := rows.Scan(&row.AdhaarNo, &row.PersonalMobile, &row.Branch, &row.EmpID, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        } else {
            if err := rows.Scan(&row.AdhaarNo, &row.Gender, &row.Branch, &row.EmpName, &row.DOB, &row.DOJ, &row.Gender, &row.LocAdd1, &row.LocAdd2, &row.LocCity, &row.LocState, &row.LocCountry, &row.PerAdd1, &row.PerAdd2, &row.PerCity, &row.PerState, &row.PerCountry, &row.OfficeMail, &row.OfficeMobile, &row.LocPIN, &row.PerPIN, &row.MotherName, &row.ESINo, &row.PAN, &row.VisaNo, &row.VisaExpiry, &row.IntegreationRef, &row.OrgID, &row.DeptID, &row.SectionID, &row.JobTypeID, &row.GradeID, &row.DesignationID, &row.CustomGroup1ID, &row.CustomGroup2ID, &row.CustomGroup3ID, &row.Card1, &row.Card2, &row.ScheduleGroupID, &row.StartShift, &row.PersonalEmail, &row.WeakOffGropuID, &row.LeaveGroup, &row.Field1, &row.PFNo, &row.BloodGroup, &row.SerialNo, &row.PassportNo, &row.PassportExpiry, &row.Dept, &row.JobType, &row.Level, &row.CostCenter, &row.FatherName, &row.EmergencyNo, &row.ConfirmationPeriod, &row.PFDate, &row.CTC, &row.FBP, &row.VarPay, &row.AdminBank, &row.EmpBank, &row.IFSC, &row.PayMode, &row.RepManager, &row.RepManEmail, &row.MaritalStatus, &row.SalaryTemp, &row.IsRepMan, &row.SpouseName, &row.Relation, &row.IsMetro, &row.HasESS, &row.HasPF, &row.PFUAN, &row.IsEPFEntitled, &row.HasESI, &row.IsPFRestricted, &row.BankAcNo, &row.Designation, &row.DeptCode, &row.Role, &row.JobTitle, &row.CustomField, &row.OnboardingStatus); err != nil {
                log.Fatal(err)
            }
            models.AllUniversal = append(models.AllUniversal, row)
        }
    }

    return models.AllUniversal
}
4

1 回答 1

0

由于我用来从数据库中获取记录的查询,我遇到了“无法获取”错误。

由于很少有列中没有存储任何值,因此它们会导致错误。然后我使用“案例”技术来解决这些错误。

虽然相同的查询在 pgadmin 查询控制台中工作。

这个案例是必需的,因为我在中间使用了 graphql,而 graphql 无法处理那些空字段,所以如果字段值为空,我返回“N/A”。

在所有这些事情之前,我将多个查询传递给一个用户定义的函数,但后来我为返回单行和多行的查询创建了不同的函数。

这件事有效并解决了所有错误。

谢谢你。

于 2020-06-17T10:16:52.213 回答