func mutations(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()

	// Get a connection so that we can get access to the Spanner specific connection interface SpannerConn.
	conn, err := db.Conn(ctx)
	if err != nil {
		return err
	}

	// Mutations can be written outside an explicit transaction using SpannerConn#Apply.
	var commitTimestamp time.Time
	if err := conn.Raw(func(driverConn interface{}) error {
		spannerConn, ok := driverConn.(spannerdriver.SpannerConn)
		if !ok {
			return fmt.Errorf("unexpected driver connection %v, expected SpannerConn", driverConn)
		}
		commitTimestamp, err = spannerConn.Apply(ctx, []*spanner.Mutation{
			spanner.Insert("Singers", []string{"SingerId", "Name"}, []interface{}{int64(1), "Richard Moore"}),
			spanner.Insert("Singers", []string{"SingerId", "Name"}, []interface{}{int64(2), "Alice Henderson"}),
		})
		return err
	}); err != nil {
		return err
	}
	fmt.Printf("The transaction with two singer mutations was committed at %v\n", commitTimestamp)

	// Mutations can also be executed as part of a read/write transaction.
	// Note: The transaction is started using the connection that we had obtained. This is necessary in order to
	// ensure that the conn.Raw call below will use the same connection as the one that just started the transaction.
	tx, err := conn.BeginTx(ctx, &sql.TxOptions{})
	if err != nil {
		return err
	}

	// Get the name of a singer and update it using a mutation.
	id := int64(1)
	row := tx.QueryRowContext(ctx, "SELECT Name FROM Singers WHERE SingerId=@id", id)
	var name string
	if err := row.Scan(&name); err != nil {
		return err
	}
	if err := conn.Raw(func(driverConn interface{}) error {
		spannerConn, ok := driverConn.(spannerdriver.SpannerConn)
		if !ok {
			return fmt.Errorf("unexpected driver connection %v, expected SpannerConn", driverConn)
		}
		return spannerConn.BufferWrite([]*spanner.Mutation{
			spanner.Update("Singers", []string{"SingerId", "Name"}, []interface{}{id, name + "-Henderson"}),
		})
	}); err != nil {
		return err
	}
	if err := tx.Commit(); err != nil {
		return err
	}
	fmt.Print("Updated the name of the first singer\n")

	// Read back the updated row.
	row = db.QueryRowContext(ctx, "SELECT SingerId, Name FROM Singers WHERE SingerId = @id", id)
	if err := row.Err(); err != nil {
		return err
	}
	if err := row.Scan(&id, &name); err != nil {
		return err
	}
	fmt.Printf("Updated singer: %v %v\n", id, name)

	return nil
}
// Example that shows how to use Mutations to insert data in a Cloud Spanner database using the Go sql driver.
// Mutations can be more efficient than DML statements for bulk insert/update operations.
//
// Execute the sample with the command `go run main.go` from this directory.
Example that shows how to use Mutations to insert data in a Cloud Spanner database using the Go sql driver.
Mutations can be more efficient than DML statements for bulk insert/update operations.

Execute the sample with the command `go run main.go` from this directory.
// Example that shows how to use Mutations to insert data in a Cloud Spanner database using the Go sql driver . // Mutations can be more efficient than DML statements for bulk insert / update operations . // // Execute the sample with the command ` go run main . go ` from this directory .
func mutations(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()
	conn, err := db.Conn(ctx)
	if err != nil {
		return err
	}
	var commitTimestamp time.Time
	if err := conn.Raw(func(driverConn interface{}) error {
		spannerConn, ok := driverConn.(spannerdriver.SpannerConn)
		if !ok {
			return fmt.Errorf("unexpected driver connection %v, expected SpannerConn", driverConn)
		}
		commitTimestamp, err = spannerConn.Apply(ctx, []*spanner.Mutation{
			spanner.Insert("Singers", []string{"SingerId", "Name"}, []interface{}{int64(1), "Richard Moore"}),
			spanner.Insert("Singers", []string{"SingerId", "Name"}, []interface{}{int64(2), "Alice Henderson"}),
		})
		return err
	}); err != nil {
		return err
	}
	fmt.Printf("The transaction with two singer mutations was committed at %v\n", commitTimestamp)
	tx, err := conn.BeginTx(ctx, &sql.TxOptions{})
	if err != nil {
		return err
	}
	id := int64(1)
	row := tx.QueryRowContext(ctx, "SELECT Name FROM Singers WHERE SingerId=@id", id)
	var name string
	if err := row.Scan(&name); err != nil {
		return err
	}
	if err := conn.Raw(func(driverConn interface{}) error {
		spannerConn, ok := driverConn.(spannerdriver.SpannerConn)
		if !ok {
			return fmt.Errorf("unexpected driver connection %v, expected SpannerConn", driverConn)
		}
		return spannerConn.BufferWrite([]*spanner.Mutation{
			spanner.Update("Singers", []string{"SingerId", "Name"}, []interface{}{id, name + "-Henderson"}),
		})
	}); err != nil {
		return err
	}
	if err := tx.Commit(); err != nil {
		return err
	}
	fmt.Print("Updated the name of the first singer\n")
	row = db.QueryRowContext(ctx, "SELECT SingerId, Name FROM Singers WHERE SingerId = @id", id)
	if err := row.Err(); err != nil {
		return err
	}
	if err := row.Scan(&id, &name); err != nil {
		return err
	}
	fmt.Printf("Updated singer: %v %v\n", id, name)
	return nil
}
func mutations ( projectId , instanceId , databaseId string ) error { ctx := context . Background ( ) db , err := sql . Open ( " " , fmt . Sprintf ( " " , projectId , instanceId , databaseId ) ) if err != nil { return err } defer db . Close ( ) conn , err := db . Conn ( ctx ) if err != nil { return err } var commitTimestamp time . Time if err := conn . Raw ( func ( driverConn interface { } ) error { spannerConn , ok := driverConn . ( spannerdriver . SpannerConn ) if ! ok { return fmt . Errorf ( " " , driverConn ) } commitTimestamp , err = spannerConn . Apply ( ctx , [ ] * spanner . Mutation { spanner . Insert ( " " , [ ] string { " " , " " } , [ ] interface { } { int64 ( 1 ) , " " } ) , spanner . Insert ( " " , [ ] string { " " , " " } , [ ] interface { } { int64 ( 2 ) , " " } ) , } ) return err } ) ; err != nil { return err } fmt . Printf ( " \n " , commitTimestamp ) tx , err := conn . BeginTx ( ctx , & sql . TxOptions { } ) if err != nil { return err } id := int64 ( 1 ) row := tx . QueryRowContext ( ctx , " " , id ) var name string if err := row . Scan ( & name ) ; err != nil { return err } if err := conn . Raw ( func ( driverConn interface { } ) error { spannerConn , ok := driverConn . ( spannerdriver . SpannerConn ) if ! ok { return fmt . Errorf ( " " , driverConn ) } return spannerConn . BufferWrite ( [ ] * spanner . Mutation { spanner . Update ( " " , [ ] string { " " , " " } , [ ] interface { } { id , name + " " } ) , } ) } ) ; err != nil { return err } if err := tx . Commit ( ) ; err != nil { return err } fmt . Print ( " \n " ) row = db . QueryRowContext ( ctx , " " , id ) if err := row . Err ( ) ; err != nil { return err } if err := row . Scan ( & id , & name ) ; err != nil { return err } fmt . Printf ( " \n " , id , name ) return nil }
Example that shows how to use Mutations to insert data in a Cloud Spanner database using the Go sql driver.
Example that shows how to use Mutations to insert data in a Cloud Spanner database using the Go sql driver .
// Get a connection so that we can get access to the Spanner specific connection interface SpannerConn. // Mutations can be written outside an explicit transaction using SpannerConn#Apply. // Mutations can also be executed as part of a read/write transaction. // Note: The transaction is started using the connection that we had obtained. This is necessary in order to // ensure that the conn.Raw call below will use the same connection as the one that just started the transaction. // Get the name of a singer and update it using a mutation. // Read back the updated row.