我正在使用 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
}