GO語言"database/sql"包中"Stmt.QueryRowContext"類型的用法及代碼示例。
用法:
func(s *Stmt) QueryRowContext(ctx context.Context, args ...any) *Row
QueryRowContext 使用給定的參數執行準備好的查詢語句。如果在語句執行期間發生錯誤,則該錯誤將通過對返回的 *Row 調用 Scan 來返回,該 *Row 始終為非零。如果查詢未選擇任何行,*Row's Scan 將返回 ErrNoRows 否則,*Row's Scan 掃描第一個選定行並丟棄其餘行。
例子:
package main
import (
"context"
"database/sql"
"log"
)
var (
ctx context.Context
db *sql.DB
)
func main() {
// In normal use, create one Stmt when your process starts.
stmt, err := db.PrepareContext(ctx, "SELECT username FROM users WHERE id = ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
// Then reuse it each time you need to issue the query.
id := 43
var username string
err = stmt.QueryRowContext(ctx, id).Scan(&username)
switch {
case err == sql.ErrNoRows:
log.Fatalf("no user with id %d", id)
case err != nil:
log.Fatal(err)
default:
log.Printf("username is %s\n", username)
}
}
相關用法
- GO Stmt用法及代碼示例
- GO StreamWriter用法及代碼示例
- GO StructTag.Lookup用法及代碼示例
- GO Strings用法及代碼示例
- GO StructTag用法及代碼示例
- GO StructOf用法及代碼示例
- GO Stringer用法及代碼示例
- GO StripPrefix用法及代碼示例
- GO StreamReader用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO Split用法及代碼示例
- GO Server.Shutdown用法及代碼示例
- GO Slice用法及代碼示例
- GO SplitAfter用法及代碼示例
- GO Sum256用法及代碼示例
- GO SectionReader用法及代碼示例
- GO Sin用法及代碼示例
- GO Sprintf用法及代碼示例
- GO SendMail用法及代碼示例
- GO Sprint用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Stmt.QueryRowContext。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。