Golang database/sql.DB.QueryRow function examples
package database/sql
QueryRow executes a query that is expected to return at most one row. QueryRow always return a non-nil value. Errors are deferred until Row's Scan method is called.
Golang database/sql.DB.QueryRow function usage examples
Example 1: ( from http://golang.org/pkg/database/sql/#DB.QueryRow )
id := 123
var username string
err := db.QueryRow("SELECT username FROM users WHERE id=?", id).Scan(&username)
switch {
case err == sql.ErrNoRows:
log.Printf("No user with that ID.")
case err != nil:
log.Fatal(err)
default:
fmt.Printf("Username is %s\n", username)
}
Example 2:
// Insert nil
b = nil
success := false
if err = dbt.db.QueryRow("SELECT ? IS NULL", b).Scan(&success); err != nil {
dbt.Fatal(err)
}
if !success {
dbt.Error("Inserting []byte(nil) as NULL failed")
}
Reference :
Advertisement
Something interesting
Tutorials
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+48.1k Golang : How to convert JSON string to map and slice
+26.8k Golang : Find files by extension
+9.8k Golang : Qt get screen resolution and display on center example
+8.2k Golang : Get final or effective URL with Request.URL example
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+7.8k Swift : Convert (cast) String to Double
+10.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+15.2k Golang : Get HTTP protocol version example
+9.4k Golang : Create unique title slugs example
+4.3k Javascript : How to show different content with noscript?
+17.9k Golang : Qt image viewer example