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
+26.4k Golang : Convert(cast) string to uint8 type and back to string
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+14.9k Golang : How to check for empty array string or string?
+7.4k Android Studio : How to detect camera, activate and capture example
+6.2k Linux/Unix : Commands that you need to be careful about
+20k Golang : How to run your code only once with sync.Once object
+9.5k Golang : Convert(cast) string to int64
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+17k Golang : Covert map/slice/array to JSON or XML format
+10.1k Golang : Print how to use flag for your application example
+16.8k Golang : Get own process identifier
+4.7k Adding Skype actions such as call and chat into web page examples