Run an SQL INSERT statement using Go

Run an SQL INSERT statement by opening and closing a connection to Cloud SQL for MySQL using Go's database/sql package.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


sqlInsert := "INSERT INTO votes(candidate, created_at, updated_at) VALUES(?, NOW(), NOW())"
if team == "TABS" || team == "SPACES" {
	if _, err := app.db.Exec(sqlInsert, team); err != nil {
		fmt.Fprintf(w, "unable to save vote: %s", err)
		return fmt.Errorf("DB.Exec: %v", err)
	fmt.Fprintf(w, "Vote successfully cast for %s!\n", team)
return nil

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.