Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Db delete query always returns true

Tags:

postgresql

go

I am using golang with postgres. I want to delete users from my table by passing in the Id for that user. When I run the code below I always get true. Even when I pass in a ID that does not exist in the table. Is that normal? Does DB.Exec return err if the user ID is not in the database?

func DeleteUser(ID int){
    err = nil
    _, err := DB.Exec("DELETE FROM Users WHERE user_id=$1", ID)

    if err == nil {

        return true
    }

    return false

}
like image 753
amanda45 Avatar asked Sep 15 '25 15:09

amanda45


1 Answers

Try this

func DeleteUser(ID int){
    err = nil
    res, err := DB.Exec("DELETE FROM Users WHERE user_id=$1", ID)

    if err == nil {

       count, err := res.RowsAffected()  
       if err == nil {
          /* check count and return true/false */
       }

    }

    return false

}
like image 78
Tarun Lalwani Avatar answered Sep 17 '25 07:09

Tarun Lalwani