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
+9k Golang : Build and compile multiple source files
+9k Golang : Populate or initialize struct with values example
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+30.4k Golang : How to redirect to new page with net/http?
+27.5k Golang : dial tcp: too many colons in address
+33.8k Golang : convert(cast) bytes to string
+5.8k Golang : List all packages and search for certain package
+19.2k Golang : Check whether a network interface is up on your machine
+8.7k Golang : Find duplicate files with filepath.Walk
+6.9k Fix sudo yum hang problem with no output or error messages
+6.6k Golang : How to validate ISBN?