亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "database/sql"

  • 概述

  • 索引

  • 示例

  • 子目錄

概述

Package sql 提供了一個圍繞SQL(或類似SQL)數(shù)據(jù)庫的通用接口。

sql 包必須與數(shù)據(jù)庫驅(qū)動程序一起使用。查看https://golang.org/s/sqldrivers獲取驅(qū)動程序列表。

不支持上下文取消的驅(qū)動程序直到查詢完成后才會返回。

有關使用示例,請參閱https://golang.org/s/sqlwiki上的wiki頁面。

索引

  • Variables

  • func Drivers() []string

  • func Register(name string, driver driver.Driver)

  • type ColumnType

  • func (ci *ColumnType) DatabaseTypeName() string

  • func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool)

  • func (ci *ColumnType) Length() (length int64, ok bool)

  • func (ci *ColumnType) Name() string

  • func (ci *ColumnType) Nullable() (nullable, ok bool)

  • func (ci *ColumnType) ScanType() reflect.Type

  • type Conn

  • func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)

  • func (c *Conn) Close() error

  • func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

  • func (c *Conn) PingContext(ctx context.Context) error

  • func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)

  • func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

  • func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

  • type DB

  • func Open(driverName, dataSourceName string) (*DB, error)

  • func (db *DB) Begin() (*Tx, error)

  • func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)

  • func (db *DB) Close() error

  • func (db *DB) Conn(ctx context.Context) (*Conn, error)

  • func (db *DB) Driver() driver.Driver

  • func (db *DB) Exec(query string, args ...interface{}) (Result, error)

  • func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

  • func (db *DB) Ping() error

  • func (db *DB) PingContext(ctx context.Context) error

  • func (db *DB) Prepare(query string) (*Stmt, error)

  • func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)

  • func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

  • func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

  • func (db *DB) QueryRow(query string, args ...interface{}) *Row

  • func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

  • func (db *DB) SetConnMaxLifetime(d time.Duration)

  • func (db *DB) SetMaxIdleConns(n int)

  • func (db *DB) SetMaxOpenConns(n int)

  • func (db *DB) Stats() DBStats

  • type DBStats

  • type IsolationLevel

  • type NamedArg

  • func Named(name string, value interface{}) NamedArg

  • type NullBool

  • func (n *NullBool) Scan(value interface{}) error

  • func (n NullBool) Value() (driver.Value, error)

  • type NullFloat64

  • func (n *NullFloat64) Scan(value interface{}) error

  • func (n NullFloat64) Value() (driver.Value, error)

  • type NullInt64

  • func (n *NullInt64) Scan(value interface{}) error

  • func (n NullInt64) Value() (driver.Value, error)

  • type NullString

  • func (ns *NullString) Scan(value interface{}) error

  • func (ns NullString) Value() (driver.Value, error)

  • type Out

  • type RawBytes

  • type Result

  • type Row

  • func (r *Row) Scan(dest ...interface{}) error

  • type Rows

  • func (rs *Rows) Close() error

  • func (rs *Rows) ColumnTypes() ([]*ColumnType, error)

  • func (rs *Rows) Columns() ([]string, error)

  • func (rs *Rows) Err() error

  • func (rs *Rows) Next() bool

  • func (rs *Rows) NextResultSet() bool

  • func (rs *Rows) Scan(dest ...interface{}) error

  • type Scanner

  • type Stmt

  • func (s *Stmt) Close() error

  • func (s *Stmt) Exec(args ...interface{}) (Result, error)

  • func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error)

  • func (s *Stmt) Query(args ...interface{}) (*Rows, error)

  • func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error)

  • func (s *Stmt) QueryRow(args ...interface{}) *Row

  • func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row

  • type Tx

  • func (tx *Tx) Commit() error

  • func (tx *Tx) Exec(query string, args ...interface{}) (Result, error)

  • func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

  • func (tx *Tx) Prepare(query string) (*Stmt, error)

  • func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

  • func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

  • func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

  • func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

  • func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

  • func (tx *Tx) Rollback() error

  • func (tx *Tx) Stmt(stmt *Stmt) *Stmt

  • func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt

  • type TxOptions

示例

DB.Query DB.QueryRow DB.Query (MultipleResultSets)

包文件

convert.go ctxutil.go sql.go

變量

在已經(jīng)提交或回滾的連接上執(zhí)行的任何操作都會返回 ErrConnDone。

var ErrConnDone = errors.New("database/sql: connection is already closed")

當 QueryRow 沒有返回行時,Scan 返回 ErrNoRows。在這種情況下,QueryRow 會返回一個占位符* Row 值,該值將推遲發(fā)生此錯誤直到掃描。

var ErrNoRows = errors.New("sql: no rows in result set")

對已經(jīng)提交或回滾的事務執(zhí)行的任何操作都會返回 ErrTxDone。

var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")

func Drivers

func Drivers() []string

驅(qū)動程序返回已注冊驅(qū)動程序名稱的排序列表。

func Register

func Register(name string, driver driver.Driver)

Register 使用提供的名稱提供數(shù)據(jù)庫驅(qū)動程序。如果使用相同的名稱調(diào)用 Register 兩次,或者驅(qū)動程序為零,則會發(fā)生混亂。

type ColumnType

ColumnType 包含列的名稱和類型。

type ColumnType struct {        // 包含過濾或未導出的字段}

func (*ColumnType) DatabaseTypeName

func (ci *ColumnType) DatabaseTypeName() string

DatabaseTypeName 返回列類型的數(shù)據(jù)庫系統(tǒng)名稱。如果返回空字符串,則不支持驅(qū)動程序類型名稱。請查閱您的驅(qū)動程序文檔以獲取驅(qū)動程序數(shù)據(jù)類型列表 不包括長度說明符。常見類型包括 “VARCHAR”,“TEXT”,“NVARCHAR”,“DECIMAL”,“BOOL”,“INT”,“BIGINT”。

func (*ColumnType) DecimalSize

func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool)

DecimalSize 返回小數(shù)類型的比例和精度。如果不適用或者不支持,那么ok 是錯誤的。

func (*ColumnType) Length

func (ci *ColumnType) Length() (length int64, ok bool)

Length 返回可變長度列類型(如文本和二進制字段類型)的列類型長度。如果類型長度沒有限制,則數(shù)值為 math.MaxInt64(任何數(shù)據(jù)庫限制仍將適用)。如果列類型不是可變長度,例如 int,或者如果驅(qū)動程序不支持,那么 ok 是 false。

func (*ColumnType) Name

func (ci *ColumnType) Name() string

Name 返回列的名稱或別名。

func (*ColumnType) Nullable

func (ci *ColumnType) Nullable() (nullable, ok bool)

Nullable 返回列是否可以為 null。如果一個驅(qū)動程序不支持這個屬性,ok 將會是 false。

func (*ColumnType) ScanType

func (ci *ColumnType) ScanType() reflect.Type

ScanType 使用 Rows.Scan 返回適合掃描的 Go 類型。如果驅(qū)動程序不支持此屬性,ScanType 將返回空接口的類型。

type Conn

Conn 代表一個數(shù)據(jù)庫會話而不是一個數(shù)據(jù)庫會話池。除非特別需要連續(xù)的單個數(shù)據(jù)庫會話,否則首選運行 DB 查詢。

Conn 必須調(diào)用 Close 來將連接返回到數(shù)據(jù)庫池,并且可以與正在運行的查詢同時進行。

在調(diào)用 Close 之后,連接上的所有操作都會因 ErrConnDone 而失敗。

type Conn struct {        // 包含過濾或未導出的字段}

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)

BeginTx 開始一個交易。

提供的上下文用于事務提交或回滾之前。如果上下文被取消,sql包將回滾事務。如果提供給 BeginTx 的上下文被取消,則Tx.Commit 將返回錯誤。

提供的 TxOptions 是可選的,如果應該使用默認值,則可能為零。如果使用驅(qū)動程序不支持的非默認隔離級別,則會返回錯誤。

func (*Conn) Close

func (c *Conn) Close() error

Close 將連接返回到連接池。Close 后的所有操作都將返回ErrConnDone。Close 是安全地與其他操作同時進行,并會阻止,直到所有其他操作完成。首先取消使用的上下文,然后直接調(diào)用 close 可能會很有用。

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

ExecContext 執(zhí)行查詢而不返回任何行。該參數(shù)適用于查詢中的任何占位符參數(shù)。

func (*Conn) PingContext

func (c *Conn) PingContext(ctx context.Context) error

PingContext 驗證到數(shù)據(jù)庫的連接是否仍然存在。

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext 為以后的查詢或執(zhí)行創(chuàng)建一個準備好的語句??梢詮姆祷氐恼Z句中同時運行多個查詢或執(zhí)行。當語句不再需要時,調(diào)用者必須調(diào)用語句的 Close 方法。

提供的上下文用于編寫聲明,而不用于執(zhí)行聲明。

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext 執(zhí)行一個返回行的查詢,通常是一個 SELECT。該參數(shù)適用于查詢中的任何占位符參數(shù)。

func (*Conn) QueryRowContext

func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext 執(zhí)行一個預計最多只返回一行的查詢。QueryRowContext 總是返回一個非零值。錯誤被推遲到行的掃描方法被調(diào)用。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

type DB

DB是一個數(shù)據(jù)庫句柄,表示一個包含零個或多個底層連接的池。多個 goroutine 并發(fā)使用是安全的。

sql 包自動創(chuàng)建并釋放連接;它也保持空閑連接的空閑池。如果數(shù)據(jù)庫具有每個連接狀態(tài)的概念,則只能在事務內(nèi)可靠地觀察到這種狀態(tài)。一旦 DB.Begin 被調(diào)用,返回的 Tx 就綁定到單個連接。在事務上調(diào)用 Commit 或 Rollback 后,該事務的連接將返回到數(shù)據(jù)庫的空閑連接池。池的大小可以通過 SetMaxIdleConns 進行控制。

type DB struct {        // 包含過濾或未導出的字段}

func Open

func Open(driverName, dataSourceName string) (*DB, error)

Open 打開一個由其數(shù)據(jù)庫驅(qū)動程序名稱和驅(qū)動程序特定的數(shù)據(jù)源名稱指定的數(shù)據(jù)庫,通常由至少一個數(shù)據(jù)庫名稱和連接信息組成。

大多數(shù)用戶將通過驅(qū)動程序特定的連接幫助程序函數(shù)打開數(shù)據(jù)庫,該函數(shù)返回一個 *DB。Go標準庫中不包含數(shù)據(jù)庫驅(qū)動程序。有關第三方驅(qū)動程序的列表,請參閱https://golang.org/s/sqldrivers。

Open 可能只是驗證其參數(shù)而不創(chuàng)建與數(shù)據(jù)庫的連接。要驗證數(shù)據(jù)源名稱是否有效,請調(diào)用 Ping。

返回的數(shù)據(jù)庫對于多個 goroutine 的并發(fā)使用是安全的,并保持其自己的空閑連接池。因此,Open 函數(shù)應該只調(diào)用一次。很少有必要關閉數(shù)據(jù)庫。

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

Begin 開始一個交易。默認隔離級別取決于驅(qū)動程序。

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)

BeginTx 開始一個交易。

提供的上下文用于事務提交或回滾之前。如果上下文被取消,sql包將回滾事務。如果提供給 BeginTx 的上下文被取消,則Tx.Commit 將返回錯誤。

提供的 TxOptions 是可選的,如果應該使用默認值,則可能為零。如果使用驅(qū)動程序不支持的非默認隔離級別,則會返回錯誤。

func (*DB) Close

func (db *DB) Close() error

Close 關閉數(shù)據(jù)庫,釋放所有打開的資源。

關閉數(shù)據(jù)庫是很少見的,因為數(shù)據(jù)庫處理意味著很長壽命并且在許多 goroutine 之間共享。

func (*DB) Conn

func (db *DB) Conn(ctx context.Context) (*Conn, error)

Conn 通過打開新連接或從連接池返回現(xiàn)有連接返回單個連接。Conn 將阻塞,直到連接返回或 ctx 被取消。在同一個 Conn 上運行的查詢將在同一個數(shù)據(jù)庫會話中運行。

通過調(diào)用 Conn.Close,每個 Conn 必須在使用后返回到數(shù)據(jù)庫池。

func (*DB) Driver

func (db *DB) Driver() driver.Driver

Driver 返回數(shù)據(jù)庫的底層驅(qū)動程序。

func (*DB) Exec

func (db *DB) Exec(query string, args ...interface{}) (Result, error)

Exec 執(zhí)行查詢而不返回任何行。該參數(shù)適用于查詢中的任何占位符參數(shù)。

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

ExecContext 執(zhí)行查詢而不返回任何行。該參數(shù)適用于查詢中的任何占位符參數(shù)。

func (*DB) Ping

func (db *DB) Ping() error

Ping 驗證與數(shù)據(jù)庫的連接仍然存在,并在必要時建立連接。

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

PingContext 驗證與數(shù)據(jù)庫的連接是否仍然存在,如果需要的話建立連接。

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, error)

Prepare 為以后的查詢或執(zhí)行創(chuàng)建一個準備好的語句??梢詮姆祷氐恼Z句中同時運行多個查詢或執(zhí)行。當語句不再需要時,調(diào)用者必須調(diào)用語句的 Close 方法。

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext 為以后的查詢或執(zhí)行創(chuàng)建一個準備好的語句??梢詮姆祷氐恼Z句中同時運行多個查詢或執(zhí)行。當語句不再需要時,調(diào)用者必須調(diào)用語句的 Close 方法。

提供的上下文用于編寫聲明,而不用于執(zhí)行聲明。

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

查詢執(zhí)行一個返回行的查詢,通常是一個 SELECT。該參數(shù)適用于查詢中的任何占位符參數(shù)。

示例

代碼:

age := 27rows, err := db.Query("SELECT name FROM users WHERE age=?", age)if err != nil {
        log.Fatal(err)}defer rows.Close()for rows.Next() {        var name string        if err := rows.Scan(&name); err != nil {
                log.Fatal(err)        }
        fmt.Printf("%s is %d\n", name, age)}if err := rows.Err(); err != nil {
        log.Fatal(err)}

示例(MultipleResultSets)

代碼:

age := 27q := `
create temp table uid (id bigint); -- Create temp table for queries.
insert into uid
select id from users where age < ?; -- Populate temp table.

-- First result set.
select
        users.id, name
from
        users
        join uid on users.id = uid.id
;

-- Second result set.
select 
        ur.user, ur.role
from
        user_roles as ur
        join uid on uid.id = ur.user
;
        `rows, err := db.Query(q, age)if err != nil {
        log.Fatal(err)}defer rows.Close()for rows.Next() {        var (
                id   int64
                name string        )        if err := rows.Scan(&id, &name); err != nil {
                log.Fatal(err)        }
        fmt.Printf("id %d name is %s\n", id, name)}if !rows.NextResultSet() {
        log.Fatal("expected more result sets", rows.Err())}var roleMap = map[int64]string{        1: "user",        2: "admin",        3: "gopher",}for rows.Next() {        var (
                id   int64
                role int64        )        if err := rows.Scan(&id, &role); err != nil {
                log.Fatal(err)        }
        fmt.Printf("id %d has role %s\n", id, roleMap[role])}if err := rows.Err(); err != nil {
        log.Fatal(err)}

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext 執(zhí)行一個返回行的查詢,通常是一個 SELECT。該參數(shù)適用于查詢中的任何占位符參數(shù)。

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *Row

QueryRow 執(zhí)行一個查詢,該查詢最多只返回一行。QueryRow總是返回一個非零值。錯誤被推遲到行的掃描方法被調(diào)用。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

示例

代碼:

id := 123var username string
err := db.QueryRow("SELECT username FROM users WHERE id=?", id).Scan(&username)switch {case err == sql.ErrNoRows:
        log.Printf("No user with that ID.")case err != nil:
        log.Fatal(err)default:
        fmt.Printf("Username is %s\n", username)}

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext 執(zhí)行一個預計最多只返回一行的查詢。QueryRowContext 總是返回一個非零值。錯誤被推遲到行的掃描方法被調(diào)用。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

func (*DB) SetConnMaxLifetime

func (db *DB) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime 設置連接可以被重用的最大時間量。

過期的連接可能會在重用之前被緩慢地關閉。

如果d <= 0,連接將永久重用。

func (*DB) SetMaxIdleConns

func (db *DB) SetMaxIdleConns(n int)

SetMaxIdleConns 設置空閑連接池中的最大連接數(shù)。

如果 MaxOpenConns 大于0但小于新的 MaxIdleConns,那么新的 MaxIdleConns 將減少以匹配 MaxOpenConns 限制

如果n <= 0,則不保留空閑連接。

func (*DB) SetMaxOpenConns

func (db *DB) SetMaxOpenConns(n int)

SetMaxOpenConns 設置打開到數(shù)據(jù)庫的最大連接數(shù)。

如果 MaxIdleConns 大于0并且新的 MaxOpenConns 小于MaxIdleConns,則 MaxIdleConns 將減少以匹配新的MaxOpenConns 限制

如果n <= 0,那么打開連接的數(shù)量沒有限制。默認值是0(無限制)。

func (*DB) Stats

func (db *DB) Stats() DBStats

Stats 返回數(shù)據(jù)庫統(tǒng)計信息

type DBStats

DBStats 包含數(shù)據(jù)庫統(tǒng)計信息。

type DBStats struct {        // OpenConnections是打開連接到數(shù)據(jù)庫的數(shù)量。
        OpenConnections int}

type IsolationLevel

IsolationLevel 是 TxOptions 中使用的事務隔離級別。

type IsolationLevel int

BeginTx 中驅(qū)動程序可能支持的各種隔離級別。如果驅(qū)動程序不支持給定的隔離級別,則可能會返回錯誤。

參見https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels(https://en.wikipedia.org/wiki/Isolation_(database_systems%29個#Isolation_levels)。

const (
        LevelDefault IsolationLevel = iota
        LevelReadUncommitted
        LevelReadCommitted
        LevelWriteCommitted
        LevelRepeatableRead
        LevelSnapshot
        LevelSerializable
        LevelLinearizable)

type NamedArg

NamedArg 是一個命名參數(shù)。NamedArg 值可以用作 Query 或Exec 的參數(shù),并綁定到 SQL 語句中的相應命名參數(shù)。

有關創(chuàng)建 NamedArg 值的更簡潔的方法,請參閱 Named 函數(shù)。

type NamedArg struct {        // Name是參數(shù)占位符的名稱。        //        // 如果為空,參數(shù)列表中的序號位置將為        // 被使用。        //        // 名稱必須省略任何符號前綴。
        Name string        // 值是參數(shù)的值。        // 它可以被分配與查詢相同的值類型        // 參數(shù)。
        Value interface{}        // 包含過濾或未導出的字段}

func Named

func Named(name string, value interface{}) NamedArg

Named 提供了一種更簡潔的方式來創(chuàng)建 NamedArg 值。

用法示例:

db.ExecContext(ctx, `
    delete from Invoice
    where
        TimeCreated < @end
        and TimeCreated >= @start;`,
    sql.Named("start", startTime),
    sql.Named("end", endTime),)

type NullBool

NullBool 表示一個可能為 null 的布爾值。NullBool 實現(xiàn)了Scanner 接口,因此它可以用作掃描目標,類似于 NullString。

type NullBool struct {
        Bool  bool
        Valid bool // 如果Bool不為NULL,則有效為真}

func (*NullBool) Scan

func (n *NullBool) Scan(value interface{}) error

Scan 實現(xiàn)掃描儀界面。

func (NullBool) Value

func (n NullBool) Value() (driver.Value, error)

Value 實現(xiàn)了驅(qū)動 Valuer 接口。

type NullFloat64

NullFloat64 表示可能為 null 的 float64。NullFloat64 實現(xiàn)了掃描器接口,因此它可以用作掃描目標,類似于 NullString。

type NullFloat64 struct {
        Float64 float64
        Valid   bool //如果Float64不為NULL,則有效}

func (*NullFloat64) Scan

func (n *NullFloat64) Scan(value interface{}) error

Scan 實現(xiàn)掃描儀界面。

func (NullFloat64) Value

func (n NullFloat64) Value() (driver.Value, error)

Value 實現(xiàn)了驅(qū)動 Valuer 接口。

type NullInt64

NullInt64 代表可能為 null 的 int64。NullInt64 實現(xiàn)了掃描器接口,因此它可以用作掃描目標,類似于 NullString。

type NullInt64 struct {
        Int64 int64
        Valid bool // 如果Int64不為NULL,則有效為真}

func (*NullInt64) Scan

func (n *NullInt64) Scan(value interface{}) error

Scan 實現(xiàn)掃描儀界面。

func (NullInt64) Value

func (n NullInt64) Value() (driver.Value, error)

Value 實現(xiàn)了驅(qū)動 Valuer 接口。

type NullString

NullString 表示一個可能為空的字符串。NullString 實現(xiàn)了掃描器接口,因此它可以用作掃描目標:

var s NullString
err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)...if s.Valid {   // 使用 s.String} else {   // NULL值(空值)}
type NullString struct {
        String string
        Valid  bool // 如果String不為NULL,則有效值為true}

func (*NullString) Scan

func (ns *NullString) Scan(value interface{}) error

Scan 實現(xiàn)掃描儀界面。

func (NullString) Value

func (ns NullString) Value() (driver.Value, error)

Value 實現(xiàn)了驅(qū)動 Valuer 接口。

type Out

Out 可用于從存儲過程中檢索 OUTPUT 值參數(shù)。

并非所有驅(qū)動程序和數(shù)據(jù)庫都支持 OUTPUT 值參數(shù)。

用法示例:

var outArg string
_, err := db.ExecContext(ctx, "ProcName", sql.Named("Arg1", Out{Dest: &outArg}))
type Out struct {        // Dest是指向?qū)⒈辉O置為結(jié)果的值的指針        // 存儲過程OUTPUT參數(shù)。
        Dest interface{}        // In是參數(shù)是否是INOUT參數(shù)。 如果是這樣,輸入值存儲        // 過程是Dest指針的取消引用值,然后將其替換為        // 輸出值。
        In bool        // 包含過濾或未導出的字段}

type RawBytes

RawBytes 是一個字節(jié)片段,用于保存對數(shù)據(jù)庫本身擁有的內(nèi)存的引用。在掃描到 RawBytes 之后,切片只有在下一次調(diào)用 Next,Scan 或 Close 時才有效。

type RawBytes []byte

type Result

Result 匯總了執(zhí)行的 SQL 命令。

type Result interface {        // LastInsertId返回數(shù)據(jù)庫生成的整數(shù)        // 以響應命令。 通常這將來自        // 插入新行時的“自動增量(auto increment)”列。 并非全部        // 數(shù)據(jù)庫支持這個功能,以及這樣的語法        // 聲明各不相同        LastInsertId() (int64, error)        // RowsAffected返回受影響的行數(shù)        // 更新,插入或刪除。 不是每個數(shù)據(jù)庫或數(shù)據(jù)庫        // 驅(qū)動可能會支持這個。        RowsAffected() (int64, error)}

type Row

Row 是調(diào)用 QueryRow 選擇單個行的結(jié)果。

type Row struct {        // 包含過濾或未導出的字段}

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

Scan 將匹配行中的列復制到 dest 指向的值中。有關詳細信息,請參閱 Rows.Scan 上的文檔。如果多個行與查詢匹配,則 Scan 使用第一行并丟棄其余行。如果沒有行與查詢匹配,則 Scan 會返回 ErrNoRows。

type Rows

Rows 是查詢的結(jié)果。它的游標在結(jié)果集的第一行之前開始。使用下一步在行中前進:

rows, err := db.Query("SELECT ...")...defer rows.Close()for rows.Next() {    var id int    var name string
    err = rows.Scan(&id, &name)    ...}err = rows.Err() // 獲取迭代期間遇到的任何錯誤
type Rows struct {        // 包含過濾或未導出的字段}

func (*Rows) Close

func (rs *Rows) Close() error

Close 關閉行,防止進一步枚舉。如果 Next 被調(diào)用并返回false,并且沒有其他結(jié)果集,則行自動關閉,并且檢查 Err 的結(jié)果就足夠了。關閉是冪等的,不會影響 Err 的結(jié)果。

func (*Rows) ColumnTypes

func (rs *Rows) ColumnTypes() ([]*ColumnType, error)

ColumnTypes 返回列信息,如列類型,長度和空值。某些驅(qū)動程序可能無法提供某些信息。

func (*Rows) Columns

func (rs *Rows) Columns() ([]string, error)

列將返回列名稱。如果行關閉,或者行來自 QueryRow 并且存在延遲錯誤,則列將返回錯誤。

func (*Rows) Err

func (rs *Rows) Err() error

Err 會返回在迭代期間遇到的錯誤(如果有的話)。Err 可能會在顯式或隱式關閉后調(diào)用。

func (*Rows) Next

func (rs *Rows) Next() bool

接下來準備下一個結(jié)果行以便用 Scan 方法讀取。它在成功時返回 true,如果沒有下一個結(jié)果行或在準備過程中發(fā)生錯誤,則返回 false。應該咨詢 Err 以區(qū)分這兩種情況。

每次調(diào)用 Scan,即使是第一次,都必須先調(diào)用 Next。

func (*Rows) NextResultSet

func (rs *Rows) NextResultSet() bool

NextResultSet 準備下一個讀取結(jié)果集。如果有進一步的結(jié)果集,則返回 true;如果沒有進一步的結(jié)果集或者有錯誤前進,則返回 false。應該參考 Err 方法來區(qū)分這兩種情況。

在調(diào)用 NextResultSet 之后,應始終在掃描之前調(diào)用 Next 方法。如果有更多的結(jié)果集,它們在結(jié)果集中可能沒有行。

func (*Rows) Scan

func (rs *Rows) Scan(dest ...interface{}) error

Scan 將當前行中的列復制到dest指向的值中。dest 中的值數(shù)量必須與行中的列數(shù)相同。

Scan 將從數(shù)據(jù)庫讀取的列轉(zhuǎn)換為以下常見的 Go 類型和 sql 包提供的特殊類型:

*string*[]byte*int, *int8, *int16, *int32, *int64*uint, *uint8, *uint16, *uint32, *uint64*bool*float32, *float64*interface{}*RawBytes
any type implementing Scanner (see Scanner docs)

在最簡單的情況下,如果源列中的值的類型是整數(shù),bool 或字符串類型 T 和 dest 的類型是 *T,則 Scan 只是通過指針分配值。

Scan 也可以在字符串和數(shù)字類型之間進行轉(zhuǎn)換,只要不會丟失任何信息。當Scan 將從數(shù)字數(shù)據(jù)庫列掃描的所有數(shù)字串化為*字符串時,將掃描為數(shù)字類型進行溢出檢查。例如,值為300的float64 或值為“300”的字符串可以掃描到 uint16,但不能掃描到uint8,但 float64(255)或“255”可掃描到uint8。一個例外是某些 float64 數(shù)字字符串的掃描可能會在字符串化時丟失信息。通常,將浮點列掃描到 *float64。

如果 dest 參數(shù)的類型為 *[]byte,則 Scan 會在該參數(shù)中保存相應數(shù)據(jù)的副本。副本由調(diào)用者擁有,可以無限期修改和保存??梢酝ㄟ^使用類型 *RawBytes 的參數(shù)來避免該副本;請參閱 RawBytes 的文檔以了解其使用限制。

如果參數(shù)具有類型 *interface{},則掃描會復制底層驅(qū)動程序提供的值而不進行轉(zhuǎn)換。從類型 []byte 的源值掃描到 *interface{}時,會創(chuàng)建片的副本,并且調(diào)用者擁有結(jié)果。

時間類型的源值可以被掃描為類型 *time.Time, *interface {},*string 或 *[]byte 的值。當轉(zhuǎn)換到后兩者時,使用 time.Format3339Nano。

可以將bool類型的源值掃描到*bool,*interface {},*string,*[]字節(jié)或 *RawBytes 類型中。

對于掃描到 *bool,源可能是true,false,1,0或 strconv.ParseBool 可解析的字符串輸入。

type Scanner

Scanner 是 Scan 使用的接口。

type Scanner interface {        // 掃描從數(shù)據(jù)庫驅(qū)動程序分配一個值。        //        // src值將是以下類型之一:        //        //    int64        //    float64        //    bool        //    []byte        //    string        //    time.Time        //    nil - for NULL values        //        // 如果該值無法存儲,則應返回錯誤        // 而不會丟失信息。        Scan(src interface{}) error}

type Stmt

Stmt 是一個準備好的聲明。Stmt 對于多個 goroutines 并發(fā)使用是安全的。

type Stmt struct {        // 包含過濾或未導出的字段}

func (*Stmt) Close

func (s *Stmt) Close() error

Close 關閉聲明。

func (*Stmt) Exec

func (s *Stmt) Exec(args ...interface{}) (Result, error)

Exec 使用給定的參數(shù)執(zhí)行一個準備好的語句,并返回一個總結(jié)語句效果的 Result 。

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error)

ExecContext 使用給定的參數(shù)執(zhí)行一個準備好的語句,并返回一個總結(jié)語句效果的 Result 。

func (*Stmt) Query

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

查詢使用給定的參數(shù)執(zhí)行準備好的查詢語句,并將查詢結(jié)果作為 *Row 返回。

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error)

QueryContext 使用給定的參數(shù)執(zhí)行準備好的查詢語句,并將查詢結(jié)果作為 *Row 返回。

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...interface{}) *Row

QueryRow 使用給定的參數(shù)執(zhí)行一個準備好的查詢語句。如果在執(zhí)行語句期間發(fā)生錯誤,則在返回的 *Row 上調(diào)用 Scan 以返回該錯誤,該行總是非零。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

用法示例:

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

func (*Stmt) QueryRowContext

func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row

QueryRowContext 使用給定的參數(shù)執(zhí)行準備好的查詢語句。如果在執(zhí)行語句期間發(fā)生錯誤,則在返回的 *Row 上調(diào)用 Scan 以返回該錯誤,該行總是非零。如果查詢未選擇行,則 *Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

用法示例:

var name string
err := nameByUseridStmt.QueryRowContext(ctx, id).Scan(&name)

type Tx

Tx 是正在進行的數(shù)據(jù)庫事務。

Transaction 必須以對 Commit 或 Rollback 的調(diào)用結(jié)束。

在調(diào)用 Commit 或 Rollback 之后,事務上的所有操作都會因ErrTxDone 而失敗。

通過調(diào)用事務的 Prepare 或 Stmt 方法為事務準備的語句通過對Commit 或 Rollback 的調(diào)用關閉。

type Tx struct {        // 包含過濾或未導出的字段}

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit 提交事務(transaction)。

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...interface{}) (Result, error)

Exec 執(zhí)行不返回行的查詢。例如:INSERT 和 UPDATE。

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)

ExecContext 執(zhí)行不返回行的查詢。例如:INSERT 和 UPDATE。

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare 創(chuàng)建一個準備好的語句,以便在事務中使用。

返回的語句在事務內(nèi)運行,并且一旦事務被提交或回滾就不能再使用。

要在此事務中使用現(xiàn)有的準備語句,請參閱 Tx.Stmt。

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext 在事務中創(chuàng)建一個準備好的語句。

返回的語句在事務內(nèi)運行,并在事務提交或回滾時關閉。

要在此事務中使用現(xiàn)有的準備語句,請參閱 Tx.Stmt。

提供的上下文將用于準備上下文,而不用于執(zhí)行返回的語句。返回的語句將在事務上下文中運行。

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

查詢執(zhí)行一個返回行的查詢,通常是一個 SELECT。

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext 執(zhí)行一個返回行的查詢,通常是一個 SELECT。

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

QueryRow 執(zhí)行一個查詢,該查詢最多只返回一行。QueryRow 總是返回一個非零值。錯誤被推遲到行的掃描方法被調(diào)用。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext 執(zhí)行一個預計最多只返回一行的查詢。QueryRowContext 總是返回一個非零值。錯誤被推遲到行的掃描方法被調(diào)用。如果查詢未選擇行,則*Row 的掃描將返回錯誤號。否則,*Row 的掃描將掃描第一個選定的行并丟棄其余行。

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback 將中止事務。

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

Stmt 從現(xiàn)有的語句中返回特定于事務的預處理語句。

例子:

updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")...tx, err := db.Begin()...res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)

返回的語句在事務內(nèi)運行,并在事務提交或回滾時關閉。

func (*Tx) StmtContext

func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt

StmtContext 從現(xiàn)有語句中返回特定于事務的預處理語句。

示例:

updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")...tx, err := db.Begin()...res, err := tx.StmtContext(ctx, updateMoney).Exec(123.45, 98293203)

返回的語句在事務內(nèi)運行,并在事務提交或回滾時關閉。

type TxOptions

TxOptions 保存要在 DB.BeginTx 中使用的事務選項。

type TxOptions struct {        // 隔離(Isolation)是事務隔離級別。        // 如果為零,則使用驅(qū)動程序或數(shù)據(jù)庫的默認級別。
        Isolation IsolationLevel
        ReadOnly  bool}

子目錄

名稱

概要

| 驅(qū)動程序|

軟件包驅(qū)動程序定義由 sql 包使用的數(shù)據(jù)庫驅(qū)動程序?qū)崿F(xiàn)的接口。

Previous article: Next article: