Database/sql not returning results as expected

Hi guys,
firstly, it’s very late and I am so tired, so my apologies for any mistakes found herein, but I think everything is sound…
I am using database/sql to make queries from my Postgres database. The queries are all working very well now too. One of my queries though deletes some tuples of data from a table. Although it works just fine, I want to be able to see how many rows were affected/deleted. This should be simple enough, and to me should work as per the code shown below:

     myResults, err := db.Exec(sqlStatement) // Create table
 	if err != nil {
		fmt.Println("Info:")
 		log.Fatal(err)
 	} else {
 		fmt.Printf("\ndb.Exec results = %d", myResults)
 		fmt.Printf("\n%d rows were deleted.", myResults.RowsAffected)
 	}

When this executes, I get the following result

Database Deleted rows

As you can see in the image, RowsAffected is not providing me with the expected result (I have no idea where this is pointing to). You will note that in the previous line (curly brackets) 8 is shown inside the brackets. This is the number I am after, and is the correct number of rows affected.

I have read the information provided here, that states the values may not be available, depending on what database type you are using. However clearly the value I want is there (in the provided example, rows deleted =8)
https://golang.org/pkg/database/sql/#Result

How do I read that number out of this variable of type sql.Results?

RowsAffected() is a method, you need to call it. The 6301440 is a decimal representation of the memory address that holds the method of your data.

Wow, thanks @NobbZ, I don’t think I would have guessed that, even if I wasn’t thoroughly tired last night! Learned something new, that a function can return not just values, but also functions/methods.

Everything is working now, and to add a little clarity for anybody else, here is the working Go code. This resolved the following error I was getting:
Printf format %v arg myResults.RowsAffected is a func value, not called go-vet

Thanks again for the help.

 myResults, err := db.Exec(sqlStatement) // Create table
     if err != nil {
         fmt.Println("Info:")
         log.Fatal(err)
     } else {
         // myResults returned by earlier function call is an INTERFACE for associated methods, not a value
         // To get the value we require, we call the relevant method
         delRows, err := myResults.RowsAffected()
         if err != nil {
             fmt.Println("Error: Failed to read count of affected rows")
             log.Fatal(err)
         }
         fmt.Printf("\n%v rows were deleted", delRows)
     }
 }

A function can return anything when its called, Even functions, yes, though that isn’t what has happened here.

What happened here is, that a “function” or a “method” is just a pointer to the memory area that has the first instruction of it.

And if you do not call it (mean writing the functions name without parenthesis), then you will get its address back, that is what the 6301440 was, a decimal representation of the functions address.

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