File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
examples/native/dynamic-scan-types Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "log"
7
+ "reflect"
8
+
9
+ "github.com/ClickHouse/clickhouse-go/v2"
10
+ )
11
+
12
+ func example () error {
13
+ conn , err := clickhouse .Open (& clickhouse.Options {
14
+ Addr : []string {"127.0.0.1:9000" },
15
+ Auth : clickhouse.Auth {
16
+ Database : "default" ,
17
+ Username : "default" ,
18
+ Password : "" ,
19
+ },
20
+ })
21
+ if err != nil {
22
+ return err
23
+ }
24
+ const query = `
25
+ SELECT
26
+ 1 AS Col1
27
+ , 'Text' AS Col2
28
+ `
29
+ rows , err := conn .Query (context .TODO (), query )
30
+ if err != nil {
31
+ return err
32
+ }
33
+ var (
34
+ columnTypes = rows .ColumnTypes ()
35
+ vars = make ([]interface {}, len (columnTypes ))
36
+ )
37
+ for i := range columnTypes {
38
+ value := reflect .New (columnTypes [i ].ScanType ()).Interface ()
39
+ vars [i ] = value
40
+ }
41
+ for rows .Next () {
42
+ if err := rows .Scan (vars ... ); err != nil {
43
+ return err
44
+ }
45
+ for _ , v := range vars {
46
+ switch v := v .(type ) {
47
+ case * string :
48
+ fmt .Println (* v )
49
+ case * uint8 :
50
+ fmt .Println (* v )
51
+ }
52
+ }
53
+ }
54
+ return nil
55
+ }
56
+ func main () {
57
+ if err := example (); err != nil {
58
+ log .Fatal (err )
59
+ }
60
+ }
You can’t perform that action at this time.
0 commit comments