GO語言"database/sql"包中"Stmt"類型的用法及代碼示例。
Stmt 是一個準備好的語句。 Stmt 對於多個 goroutine 並發使用是安全的。
如果在 Tx 或 Conn 上準備了 Stmt,它將永遠綁定到單個底層連接。如果 Tx 或 Conn 關閉,Stmt 將變得不可用,所有操作都將返回錯誤。如果在 DB 上準備了 Stmt,它將在 DB 的生命周期內保持可用。當 Stmt 需要在新的底層連接上執行時,它會自動在新的連接上進行準備。
用法:
type Stmt struct {
// contains filtered or unexported fields
}
例子:
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.QueryRowContext用法及代碼示例
- 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。