英文:
Error SQL NO result rows in set. when i input from postman with json it has error result in a rows
问题
var (
newServices *models.UpsertIntoServices
companyCode, serviceCode, approvalEmail, approvalName, coApprovalEmail, coApprovalName, technicianEmail, technicianName string
departmentApproval, companyApproval, xapiensApproval, isActual, isMinus bool
id uint
)
tx, txErr := s.db.Begin()
if txErr != nil {
return newServices, txErr
}
qapi := `
insert into company_services
(company_code, service_code, department_approval, company_approval, xapiens_approval, approval_email, approval_name, co_approval_email,technician_email, co_approval_name, technician_name, is_actual, is_minus)
values
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
on conflict (company_code, service_code)
do update set company_code =excluded.company_code, service_code =excluded.service_code;
`
//insert data into new_services
sqlErr := tx.QueryRowContext(ctx, qapi, data.CompanyCode, data.ServiceCode, data.DepartmentApproval, data.CompanyApproval, data.XapiensApproval, data.ApprovalEmail, data.ApprovalName, data.CoApprovalEmail, data.TechnicianEmail, data.CoApprovalName, data.TechnicianName, data.IsActual, data.IsMinus).Scan(
&id,
&companyCode,
&serviceCode,
&departmentApproval,
&companyApproval,
&xapiensApproval,
&approvalEmail,
&approvalName,
&coApprovalEmail,
&technicianEmail,
&coApprovalName,
&technicianName,
&isActual,
&isMinus,
)
// checking if its rows has any errors but no result here
if sqlErr != nil {
tx.Rollback()
log.Println("sql Error on Repository Upsert Flagging", sqlErr)
return newServices, sqlErr
}
// commit transaction query
txErr = tx.Commit()
if txErr != nil {
return newServices, txErr
}
这是一段Go代码,它包含了一些变量声明和数据库操作。代码的主要功能是将数据插入到数据库表company_services
中,并在冲突时执行更新操作。具体步骤如下:
- 开始数据库事务。
- 准备SQL语句
qapi
,用于插入数据或执行更新操作。 - 使用
tx.QueryRowContext
方法执行SQL语句,并将结果扫描到相应的变量中。 - 检查是否有SQL错误,如果有则回滚事务并返回错误。
- 提交事务。
- 返回结果或错误。
请注意,这段代码中的变量和数据是根据上下文提供的,因此无法确定具体的值和类型。
英文:
var (
newServices *models.UpsertIntoServices
companyCode, serviceCode, approvalEmail, approvalName, coApprovalEmail, coApprovalName, technicianEmail, technicianName string
departmentApproval, companyApproval, xapiensApproval, isActual, isMinus bool
id uint
)
tx, txErr := s.db.Begin()
if txErr != nil {
return newServices, txErr
}
qapi := `
insert into company_services
(company_code, service_code, department_approval, company_approval, xapiens_approval, approval_email, approval_name, co_approval_email,technician_email, co_approval_name, technician_name, is_actual, is_minus)
values
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
on conflict (company_code, service_code)
do update set company_code =excluded.company_code, service_code =excluded.service_code;
`
//insert data into new_services
sqlErr := tx.QueryRowContext(ctx, qapi, data.CompanyCode, data.ServiceCode, data.DepartmentApproval, data.CompanyApproval, data.XapiensApproval, data.ApprovalEmail, data.ApprovalName, data.CoApprovalEmail, data.TechnicianEmail, data.CoApprovalName, data.TechnicianName, data.IsActual, data.IsMinus).Scan(
&id,
&companyCode,
&serviceCode,
&departmentApproval,
&companyApproval,
&xapiensApproval,
&approvalEmail,
&approvalName,
&coApprovalEmail,
&technicianEmail,
&coApprovalName,
&technicianName,
&isActual,
&isMinus,
)
// checking if its rows has any errors but no result here
if sqlErr != nil {
tx.Rollback()
log.Println("sql Error on Repository Upsert Flagging", sqlErr)
return newServices, sqlErr
}
// commit transaction query
txErr = tx.Commit()
if txErr != nil {
return newServices, txErr
}
答案1
得分: 0
使用ExecContext来执行插入/更新查询。尝试以下代码:
var (
newServices *models.UpsertIntoServices
companyCode, serviceCode, approvalEmail, approvalName, coApprovalEmail, coApprovalName, technicianEmail, technicianName string
departmentApproval, companyApproval, xapiensApproval, isActual, isMinus bool
id uint
)
tx, txErr := s.db.Begin()
if txErr != nil {
return newServices, txErr
}
qapi := `
insert into company_services
(company_code, service_code, department_approval, company_approval, xapiens_approval, approval_email, approval_name, co_approval_email,technician_email, co_approval_name, technician_name, is_actual, is_minus)
values
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
on conflict (company_code, service_code)
do update set company_code =excluded.company_code, service_code =excluded.service_code;
`
//insert data into new_services
result, sqlErr := tx.ExecContext(ctx, qapi, data.CompanyCode, data.ServiceCode, data.DepartmentApproval, data.CompanyApproval, data.XapiensApproval, data.ApprovalEmail, data.ApprovalName, data.CoApprovalEmail, data.TechnicianEmail, data.CoApprovalName, data.TechnicianName, data.IsActual, data.IsMinus)
// checking if its rows has any errors but no result here
if sqlErr != nil {
tx.Rollback()
log.Println("sql Error on Repository Upsert Flagging", sqlErr)
return newServices, sqlErr
}
//You can know lastInsertId and affectedRows
lastInsertId, _ := result.LastInsertId()
affectedRowsCnt,_ := result.RowsAffected()
fmt.Println(lastInsertId,affectedRowsCnt)
// commit transaction query
txErr = tx.Commit()
if txErr != nil {
return newServices, txErr
}
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
Use ExecContext to execute insert/update query. Try the below code
var (
newServices *models.UpsertIntoServices
companyCode, serviceCode, approvalEmail, approvalName, coApprovalEmail, coApprovalName, technicianEmail, technicianName string
departmentApproval, companyApproval, xapiensApproval, isActual, isMinus bool
id uint
)
tx, txErr := s.db.Begin()
if txErr != nil {
return newServices, txErr
}
qapi := `
insert into company_services
(company_code, service_code, department_approval, company_approval, xapiens_approval, approval_email, approval_name, co_approval_email,technician_email, co_approval_name, technician_name, is_actual, is_minus)
values
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
on conflict (company_code, service_code)
do update set company_code =excluded.company_code, service_code =excluded.service_code;
`
//insert data into new_services
result, sqlErr := tx.ExecContext(ctx, qapi, data.CompanyCode, data.ServiceCode, data.DepartmentApproval, data.CompanyApproval, data.XapiensApproval, data.ApprovalEmail, data.ApprovalName, data.CoApprovalEmail, data.TechnicianEmail, data.CoApprovalName, data.TechnicianName, data.IsActual, data.IsMinus)
// checking if its rows has any errors but no result here
if sqlErr != nil {
tx.Rollback()
log.Println("sql Error on Repository Upsert Flagging", sqlErr)
return newServices, sqlErr
}
//You can know lastInsertId and affectedRows
lastInsertId, _ := result.LastInsertId()
affectedRowsCnt,_ := result.RowsAffected()
fmt.Println(lastInsertId,affectedRowsCnt)
// commit transaction query
txErr = tx.Commit()
if txErr != nil {
return newServices, txErr
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论