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
+6.5k Golang : Handling image beyond OpenCV video capture boundary
+11k Golang : Generate random elements without repetition or duplicate
+12.3k Golang : Display list of countries and ISO codes
+10.6k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+24.5k Golang : Time slice or date sort and reverse sort example
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+9.5k Golang : Convert(cast) string to int64
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+6.9k Nginx : Password protect a directory/folder
+5.7k Golang : Error handling methods