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
+12.6k Golang : flag provided but not defined error
+16.6k Golang : Delete files by extension
+19.6k Golang : Close channel after ticker stopped example
+13.5k Golang : Read XML elements data with xml.CharData example
+6.8k Get Facebook friends working in same company
+32.5k Golang : Copy directory - including sub-directories and files
+12.2k Golang : calculate elapsed run time
+30.6k Golang : Remove characters from string example
+7.6k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+5.6k Swift : Get substring with rangeOfString() function example
+27.6k Golang : dial tcp: too many colons in address
+14.2k Golang : Convert IP version 6 address to integer or decimal number