|
11 | 11 |
|
12 | 12 | Supports `table`, `discovery`, `coordination`, `ratelimiter`, `scheme` and `scripting` clients for `YDB`. |
13 | 13 |
|
14 | | -```go |
15 | | -import ( |
16 | | - "github.com/ydb-platform/ydb-go-sdk/v3" |
17 | | - "github.com/ydb-platform/ydb-go-sdk/v3/table" |
18 | | - "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named" |
19 | | - "github.com/ydb-platform/ydb-go-sdk/v3/table/types" |
20 | | -) |
21 | | - |
22 | | -db, err := ydb.New(ctx, |
23 | | - ydb.WithConnectionString("grpcs://localhost:2135/?database=/local"), |
24 | | - ydb.WithAnonymousCredentials(), |
25 | | -) |
26 | | -if err != nil { |
27 | | - log.Fatal(err) |
28 | | -} |
29 | | -defer func() { _ = db.Close(ctx) }() |
30 | | -err = db.Table().Do( |
31 | | - ctx, |
32 | | - func(ctx context.Context, s table.Session) (err error) { |
33 | | - _, res, err := s.Execute(ctx, |
34 | | - table.TxControl( |
35 | | - table.BeginTx(table.WithSerializableReadWrite()), table.CommitTx(), |
36 | | - ), |
37 | | - "DECLARE $myStr AS Utf8; SELECT 42 as id, $myStr as myStr", |
38 | | - table.NewQueryParameters( |
39 | | - table.ValueParam("$myStr", types.UTF8Value("test")), |
40 | | - ), |
41 | | - ) |
42 | | - if err != nil { |
43 | | - return err // for driver retry |
44 | | - } |
45 | | - defer func() { _ = res.Close() }() |
46 | | - var ( |
47 | | - id int32 |
48 | | - myStr *string //optional value |
49 | | - ) |
50 | | - for res.NextResultSet(ctx) { |
51 | | - for res.NextRow() { |
52 | | - err := res.ScanNamed( |
53 | | - named.Required("id", &id), |
54 | | - named.Optional("myStr", &myStr), |
55 | | - ) |
56 | | - if err != nil { |
57 | | - return err |
58 | | - } |
59 | | - fmt.Printf("got id %v, got mystr: %v\n", id, *myStr) |
60 | | - } |
61 | | - } |
62 | | - return res.Err() |
63 | | - }, |
64 | | -) |
65 | | -if err != nil { |
66 | | - log.Fatal(err) |
67 | | -} |
68 | | -``` |
69 | | -More examples of usage placed in [examples](https://github.com/ydb-platform/ydb-go-examples) repository. |
| 14 | +Simple example see in [example](example_table_test.go). More examples of usage placed in [examples](https://github.com/ydb-platform/ydb-go-examples) repository. |
70 | 15 |
|
71 | 16 | See also [CREDENTIALS.md](CREDENTIALS.md) about supported YDB credentials. |
72 | 17 |
|
|
0 commit comments