Golang database/sql.Stmt.QueryRow function examples

package database/sql

QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

Golang database/sql.Stmt.QueryRow function usage examples

Example 1:

 var name string
 err := nameByUseridStmt.QueryRow(id).Scan(&name)

Example 2:

 var (
  SELECT_BY_BANK_CODE = "SELECT 1 FROM BANK_DATA WHERE bankcode = ? and country = ?;"
  SELECT_BY_BANK_CODE_STMT *sql.Stmt
 )

 // describes the structure of an IBAN
 type Iban struct {
  countryCode string
  checkDigit  string
  bban string
  original string
  bic string
 }

 bankCode := iban.bban[0:length]
 var res int
 err := SELECT_BY_BANK_CODE_STMT.QueryRow(bankCode, iban.countryCode).Scan(&res) //< -- here

References :

https://github.com/fourcube/goiban/blob/master/bankcodevalidation.go

http://golang.org/pkg/database/sql/#Stmt.QueryRow

Advertisement