Skip to content

Commit 0c5844e

Browse files
committed
parameters example
Signed-off-by: nithinkdb <[email protected]>
1 parent 5a3a210 commit 0c5844e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/parameters/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"log"
8+
"os"
9+
"strconv"
10+
11+
dbsql "github.com/databricks/databricks-sql-go"
12+
"github.com/joho/godotenv"
13+
)
14+
15+
func main() {
16+
// Opening a driver typically will not attempt to connect to the database.
17+
err := godotenv.Load()
18+
19+
if err != nil {
20+
log.Fatal(err.Error())
21+
}
22+
port, err := strconv.Atoi(os.Getenv("DATABRICKS_PORT"))
23+
if err != nil {
24+
log.Fatal(err.Error())
25+
}
26+
connector, err := dbsql.NewConnector(
27+
dbsql.WithServerHostname(os.Getenv("DATABRICKS_HOST")),
28+
dbsql.WithPort(port),
29+
dbsql.WithHTTPPath(os.Getenv("DATABRICKS_HTTPPATH")),
30+
dbsql.WithAccessToken(os.Getenv("DATABRICKS_ACCESSTOKEN")),
31+
)
32+
if err != nil {
33+
// This will not be a connection error, but a DSN parse error or
34+
// another initialization error.
35+
log.Fatal(err)
36+
}
37+
db := sql.OpenDB(connector)
38+
defer db.Close()
39+
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
40+
// defer cancel()
41+
ctx := context.Background()
42+
var res string
43+
err1 := db.QueryRowContext(ctx, `SELECT
44+
:p_bool AS col_bool,
45+
:p_int AS col_int,
46+
:p_double AS col_double,
47+
:p_floatfloat AS col_float`,
48+
dbsql.DBSqlParam{Name: "p_bool", Value: true},
49+
dbsql.DBSqlParam{Name: "p_int", Value: int(1234)},
50+
dbsql.DBSqlParam{Name: "p_double", Type: dbsql.Double, Value: 3.14},
51+
dbsql.DBSqlParam{Name: "p_float", Type: dbsql.Float, Value: 3.14}).Scan(&res)
52+
53+
if err1 != nil {
54+
if err1 == sql.ErrNoRows {
55+
fmt.Println("not found")
56+
return
57+
} else {
58+
fmt.Printf("err: %v\n", err1)
59+
}
60+
}
61+
fmt.Println(res)
62+
63+
}

0 commit comments

Comments
 (0)