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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。