错误:SQL结果集中没有行。当我使用Postman输入JSON时,出现了错误结果行。

huangapple go评论75阅读模式
英文:

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中,并在冲突时执行更新操作。具体步骤如下:

  1. 开始数据库事务。
  2. 准备SQL语句qapi,用于插入数据或执行更新操作。
  3. 使用tx.QueryRowContext方法执行SQL语句,并将结果扫描到相应的变量中。
  4. 检查是否有SQL错误,如果有则回滚事务并返回错误。
  5. 提交事务。
  6. 返回结果或错误。

请注意,这段代码中的变量和数据是根据上下文提供的,因此无法确定具体的值和类型。

英文:
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
}

huangapple
  • 本文由 发表于 2021年12月15日 09:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/70357620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定