猿问

错误 SQL 在集合中没有结果行。当我从邮递员使用 json 输入时,它的错误结果是一行

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

}


SMILET
浏览 132回答 1
1回答

梦里花落0921

使用 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_servicesresult, 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 hereif sqlErr != nil {    tx.Rollback()    log.Println("sql Error on Repository Upsert Flagging", sqlErr)    return newServices, sqlErr}//You can know lastInsertId and affectedRowslastInsertId, _ := result.LastInsertId()affectedRowsCnt,_ :=  result.RowsAffected()fmt.Println(lastInsertId,affectedRowsCnt)// commit transaction querytxErr = tx.Commit()if txErr != nil {    return newServices, txErr}
随时随地看视频慕课网APP

相关分类

Go
我要回答