0

I'm trying to run the following query using gorp library with mysql

query = "SELECT SUM(outputoctets) FROM monthlyacct where date >= ? AND date < ? AND location = ?"
count, err := dbMap.SelectInt(query , constrains.From, constrains.To, constrains.LocationId)

This query throws the following error,

Scan error on column index 0: converting string \"\u003cnil\u003e\" to a int64: strconv.ParseInt: parsing \"\u003cnil\u003e\": invalid syntax

Here column outputoctets is type BIGINT. I also tried changing SelectInt to SelectStr, then it threw the following error;

Scan error on column index 0: unsupported driver -\u003e Scan pair: \u003cnil\u003e -\u003e *string

When I ran the query without location filter, it worked. Seems like this is due to some NULL values present in the location column. However I need to have the location filter on the query. How can I solve this?

3
  • What does MySQL output for the query? Commented Oct 12, 2015 at 1:52
  • It's always a number SUM(outputoctets) Commented Oct 12, 2015 at 4:38
  • SUM() returns NULL if there were no matching rows Commented Oct 12, 2015 at 5:28

1 Answer 1

1

Looks like the additional location criteria is filtering out all results, so the sum(outputoctets) is null. \u003cnil\u003e is the string "<nil>", which go rejects as an invalid integer.

Use a 'nullable' type to hold the sum, eg, NullInt64

Alternatively, use the mysql IFNULL function to avoid returning a null from the query, eg, IFNULL(sum(outputoctets),0)

See the Go database tutorial for more information

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.