-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet.go
35 lines (31 loc) · 805 Bytes
/
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package spanner
import (
"context"
"fmt"
"io"
"cloud.google.com/go/spanner"
)
func writeUsingDML(w io.Writer, db string) error {
ctx := context.Background()
client, err := spanner.NewClient(ctx, db)
if err != nil {
return err
}
defer client.Close()
_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
stmt := spanner.Statement{
SQL: `INSERT user (id, name) VALUES
(2, 'Melissa'),
(3, 'Russell'),
(4, 'Jacqueline'),
(5, 'Dylan')`,
}
rowCount, err := txn.Update(ctx, stmt)
if err != nil {
return err
}
fmt.Fprintf(w, "%d record(s) inserted.\n", rowCount)
return err
})
return err
}