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
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+20.9k Golang : Convert PNG transparent background image to JPG or JPEG image
+23.9k Golang : Use regular expression to validate domain name
+6.5k Golang : Handling image beyond OpenCV video capture boundary
+5.2k Responsive Google Adsense
+9.5k Golang : Convert(cast) string to int64
+4.6k Linux : sudo yum updates not working
+13.9k Golang : How to check if a file is hidden?
+8.3k Golang : Number guessing game with user input verification example
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+9.3k Golang : How to get ECDSA curve and parameters data?