GO語言"database/sql"包中"DB.QueryRowContext"類型的用法及代碼示例。
用法:
func(db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *Row
QueryRowContext 執行預計最多返回一行的查詢。 QueryRowContext 始終返回非零值。錯誤會延遲到調用 Row 的 Scan 方法。如果查詢未選擇任何行,*Row's Scan 將返回 ErrNoRows 否則,*Row's Scan 掃描第一個選定行並丟棄其餘行。
例子:
package main
import (
"context"
"database/sql"
"log"
"time"
)
var (
ctx context.Context
db *sql.DB
)
func main() {
id := 123
var username string
var created time.Time
err := db.QueryRowContext(ctx, "SELECT username, created_at FROM users WHERE id=?", id).Scan(&username, &created)
switch {
case err == sql.ErrNoRows:
log.Printf("no user with id %d\n", id)
case err != nil:
log.Fatalf("query error: %v\n", err)
default:
log.Printf("username is %q, account created on %s\n", username, created)
}
}
相關用法
- GO DB.QueryContext用法及代碼示例
- GO DB.Query用法及代碼示例
- GO DB.ExecContext用法及代碼示例
- GO DB.BeginTx用法及代碼示例
- GO DB.Prepare用法及代碼示例
- GO DB.PingContext用法及代碼示例
- GO DecodeLastRuneInString用法及代碼示例
- GO DumpResponse用法及代碼示例
- GO Date用法及代碼示例
- GO Dial用法及代碼示例
- GO Decoder.Token用法及代碼示例
- GO Decoder.Decode用法及代碼示例
- GO DumpRequest用法及代碼示例
- GO Drawer用法及代碼示例
- GO Duration.Hours用法及代碼示例
- GO Duration.Round用法及代碼示例
- GO DecodeRuneInString用法及代碼示例
- GO DumpRequestOut用法及代碼示例
- GO DecodeRune用法及代碼示例
- GO Duration用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 DB.QueryRowContext。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。