Golang database/sql.DB.Query function examples
package database/sql
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
Golang database/sql.DB.Query function usage examples
Example 1: ( from http://golang.org/pkg/database/sql/#DB.Query )
age := 27
rows, err := db.Query("SELECT name FROM users WHERE age=?", age)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
fmt.Printf("%s is %d\n", name, age)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
Example 2:
microsecsSupported := false
zeroDateSupported := false
var rows *sql.Rows
var err error
rows, err = dbt.db.Query(`SELECT cast("00:00:00.1" as TIME(1)) = "00:00:00.1"`)
if err == nil {
rows.Scan(µsecsSupported)
rows.Close()
}
rows, err = dbt.db.Query(`SELECT cast("0000-00-00" as DATE) = "0000-00-00"`)
if err == nil {
rows.Scan(&zeroDateSupported)
rows.Close()
}
References :
https://github.com/go-sql-driver/mysql/blob/master/driver_test.go
Advertisement
Something interesting
Tutorials
+34k Golang : Proper way to set function argument default value
+12.8k Golang : Listen and Serve on sub domain example
+9.2k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+19.2k Golang : Populate dropdown with html/template example
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+9.4k Golang : Terminate-stay-resident or daemonize your program?
+5.2k Golang : Issue HTTP commands to server and port example
+14.8k Golang : Find commonalities in two slices or arrays example
+28.7k Golang : Detect (OS) Operating System
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+21.1k Golang : Get password from console input without echo or masked