forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PECO-1048] Add example for parameterized queries (databricks#168)
We don't currently have an example for parameterized queries, we should add one.
- Loading branch information
Showing
4 changed files
with
167 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
dbsql "github.com/databricks/databricks-sql-go" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
func main() { | ||
// Opening a driver typically will not attempt to connect to the database. | ||
err := godotenv.Load() | ||
|
||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
port, err := strconv.Atoi(os.Getenv("DATABRICKS_PORT")) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
connector, err := dbsql.NewConnector( | ||
dbsql.WithServerHostname(os.Getenv("DATABRICKS_HOST")), | ||
dbsql.WithPort(port), | ||
dbsql.WithHTTPPath(os.Getenv("DATABRICKS_HTTPPATH")), | ||
dbsql.WithAccessToken(os.Getenv("DATABRICKS_ACCESSTOKEN")), | ||
) | ||
if err != nil { | ||
// This will not be a connection error, but a DSN parse error or | ||
// another initialization error. | ||
log.Fatal(err) | ||
} | ||
db := sql.OpenDB(connector) | ||
defer db.Close() | ||
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
// defer cancel() | ||
ctx := context.Background() | ||
var p_bool bool | ||
var p_int int | ||
var p_double float64 | ||
var p_float float32 | ||
var p_date string | ||
err1 := db.QueryRowContext(ctx, `SELECT | ||
:p_bool AS col_bool, | ||
:p_int AS col_int, | ||
:p_double AS col_double, | ||
:p_float AS col_float, | ||
:p_date AS col_date`, | ||
dbsql.Parameter{Name: "p_bool", Value: true}, | ||
dbsql.Parameter{Name: "p_int", Value: int(1234)}, | ||
dbsql.Parameter{Name: "p_double", Type: dbsql.SqlDouble, Value: "3.14"}, | ||
dbsql.Parameter{Name: "p_float", Type: dbsql.SqlFloat, Value: "3.14"}, | ||
dbsql.Parameter{Name: "p_date", Type: dbsql.SqlDate, Value: "2017-07-23 00:00:00"}).Scan(&p_bool, &p_int, &p_double, &p_float, &p_date) | ||
|
||
if err1 != nil { | ||
if err1 == sql.ErrNoRows { | ||
fmt.Println("not found") | ||
return | ||
} else { | ||
fmt.Printf("err: %v\n", err1) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters