Golang: Get Results from a MySQL Stored Procedure with Parameters

After much research and thought, I figured out how to call a MySQL stored procedure and get the results from it. The key is using multiStatements=true and autocommit=true in the connection string.

Make sure your stored procedure returns a result set, like:
...
    -- return one result set
    SELECT v_record_id AS id;
END;;

Connection string:
myDb, err := sql.Open("mysql", "user:password@/somedb?multiStatements=true&autocommit=true")

Golang query code:
var id sql.NullInt64
row := myDb.QueryRow(
  "CALL SaveUrl(?, ?)",
  data.URL,
  data.Title,
)

if err := row.Scan(&id); err != nil {
  return -1, fmt.Errorf("unable to save URL: %s", err)
}

More detailed Github Gist code sample.

I hope it helps someone else.

Soli Deo gloria

Comments

Popular posts from this blog

Text Sherlock the OpenGrok alternative (Source Code Search Engine)

Python SUDS with Windows Authentication (SOAP)

SimpleUnitTest - Easy iPhone and iPad Unit Test