3030./chdb-go --path /tmp/chdb # interactive persistent mode
3131```
3232
33- 3 . Shortcuts
34-
35- - ` \l ` to list databases;
36- - ` \dt dbname ` to list tables in database;
37-
3833``` bash
3934chdb-io/chdb-go [main] » ./chdb-go
4035Enter your SQL commands; type ' exit' to quit.
4136 :) CREATE DATABASE IF NOT EXISTS testdb;
4237
43- :) \l
44- ┏━━━━━━━━━━━━━━━━━━━━┓
45- ┃ name ┃
46- ┡━━━━━━━━━━━━━━━━━━━━┩
47- │ INFORMATION_SCHEMA │
48- ├────────────────────┤
49- │ _local │
50- ├────────────────────┤
51- │ information_schema │
52- ├────────────────────┤
53- │ system │
54- ├────────────────────┤
55- │ testdb │
56- └────────────────────┘
57-
58- :) CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree ()
59- :-] ORDER BY id;
60-
61- :) \d t testdb
62- ┏━━━━━━━━━━━┓
63- ┃ name ┃
64- ┡━━━━━━━━━━━┩
65- │ testtable │
66- └───────────┘
6738
6839```
6940
@@ -72,34 +43,50 @@ Enter your SQL commands; type 'exit' to quit.
7243package main
7344
7445import (
75- " fmt"
76- " github.com/chdb-io/chdb-go/chdb"
46+ " fmt"
47+ " os"
48+ " path/filepath"
49+
50+ " github.com/chdb-io/chdb-go/chdb"
7751)
7852
7953func main () {
80- // Stateless Query (ephemeral)
81- result := chdb.Query (" SELECT version()" , " CSV" )
82- fmt.Println (result)
83-
84- // Stateful Query (persistent)
85- session , _ := NewSession (path)
86- defer session.Cleanup ()
87-
88- session.Query (" CREATE DATABASE IF NOT EXISTS testdb; " +
89- " CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;" )
90-
91- session.Query (" USE testdb; INSERT INTO testtable VALUES (1), (2), (3);" )
92-
93- ret := session.Query (" SELECT * FROM testtable;" )
94- fmt.Println (ret)
54+ // Stateless Query (ephemeral)
55+ result , err := chdb.Query (" SELECT version()" , " CSV" )
56+ if err != nil {
57+ fmt.Println (err)
58+ }
59+ fmt.Println (result)
60+
61+ tmp_path := filepath.Join (os.TempDir (), " chdb_test" )
62+ defer os.RemoveAll (tmp_path)
63+ // Stateful Query (persistent)
64+ session , _ := chdb.NewSession (tmp_path)
65+ defer session.Cleanup ()
66+
67+ _, err = session.Query (" CREATE DATABASE IF NOT EXISTS testdb; " +
68+ " CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;" )
69+ if err != nil {
70+ fmt.Println (err)
71+ return
72+ }
73+
74+ _, err = session.Query (" USE testdb; INSERT INTO testtable VALUES (1), (2), (3);" )
75+ if err != nil {
76+ fmt.Println (err)
77+ return
78+ }
79+
80+ ret , err := session.Query (" SELECT * FROM testtable;" )
81+ if err != nil {
82+ fmt.Println (err)
83+ } else {
84+ fmt.Println (ret)
85+ }
9586}
9687```
9788
9889### Golang API docs
9990
10091- See [ lowApi.md] ( lowApi.md ) for the low level APIs.
10192- See [ chdb.md] ( chdb.md ) for high level APIs.
102-
103- ### Thanks
104-
105- - cli implementation is based on [ clickhouse-cli] ( https://github.com/memlimit/clickhouse-cli )
0 commit comments