Mysql rowsaffected return zero

_ github.com/go-sql-driver/mysql
github.com/jmoiron/sqlx

we are try to delete or update and try to find the affected rows . It returning zero.

Can you please share your code? And have you checked if perhaps an error was returned instead? Have you checked if the data got indeed removed from the table?

1 Like
tx, err := Mydb.Beginx()
if err != nil {
	return false, err
}
defer func(tx *sqlx.Tx) {
	if err != nil {
		_ = tx.Rollback()
	} else {
		_ = tx.Commit()
	}
}(tx)

_Query := `delete from emp where emp_id = ? `

rowsaffected, err := tx.Exec(_Query, _in.Emp_id)
if err != nil {
	return false, err
}

nRows, err := rowsaffected.RowsAffected()
if err != nil {
	//fmt.Println("Error: Failed to read count of affected rows")
	log.Fatal(err)
	return false, err
}
if nRows <= 0 {
	//return false, err
}

Okay, seems as if you check the errors.

As you say the number of affected rows returns zero, I assume there is no error until then, so can you please answer the still open question, whether or not you checked the database if there has actually been something data removed?

I have checked record already exist .

I have solved this issue by changing following code

tx, err := easyglobal.Mydb.Begin()
if err != nil {
	return false, err
}
defer func(tx *sql.Tx) {
	if err != nil {
		_ = tx.Rollback()
	} else {
		_ = tx.Commit()
	}
}(tx)

Instead off beingX change to begin and Sqlx to sql

please explain me in detail if you know what is reason.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.