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
+5.6k Swift : Get substring with rangeOfString() function example
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+7.9k Swift : Convert (cast) String to Float
+6.9k Mac OSX : Find large files by size
+6.6k Golang : How to determine if request or crawl is from Google robots
+12.8k Golang : Convert int(year) to time.Time type
+35.9k Golang : Integer is between a range
+3.7k Java : Random alphabets, alpha-numeric or numbers only string generator
+8.6k Golang : Progress bar with ∎ character