database/sql
import "database/sql"
Package sql provides a generic interface around SQL (or SQL-like) databases. The sql package must be used in conjunction with a database driver. See https://golang.org/s/sqldrivers for a list of drivers. Drivers that do not support context cancellation will not return until after the query is completed. For usage examples, see the wiki page at https://golang.org/s/sqlwiki.
Go 1.26.1
Variables
var ErrConnDone = errors.New("sql: connection is already closed")ErrConnDone is returned by any operation that is performed on a connection that has already been returned to the connection pool.
database/sql/sql.go:1936
var ErrNoRows = errors.New("sql: no rows in result set")ErrNoRows is returned by [Row.Scan] when [DB.QueryRow] doesn't return a row. In such a case, QueryRow returns a placeholder [*Row] value that defers this error until a Scan.
database/sql/sql.go:493
var ErrTxDone = errors.New("sql: transaction has already been committed or rolled back")ErrTxDone is returned by any operation that is performed on a transaction that has already been committed or rolled back.
database/sql/sql.go:2230
Functions
func Drivers() []string
Drivers returns a sorted list of the names of the registered drivers.
database/sql/sql.go:76
func Named(name string, value any) NamedArg
Named provides a more concise way to create [NamedArg] values. Example usage: db.ExecContext(ctx, ` delete from Invoice where TimeCreated < @end and TimeCreated >= @start;`, sql.Named("start", startTime), sql.Named("end", endTime), )
database/sql/sql.go:117
func Open(driverName string, dataSourceName string) (*DB, error)
Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information. Most users will open a database via a driver-specific connection helper function that returns a [*DB]. No database drivers are included in the Go standard library. See https://golang.org/s/sqldrivers for a list of third-party drivers. Open may just validate its arguments without creating a connection to the database. T...
database/sql/sql.go:863
func OpenDB(c driver.Connector) *DB
OpenDB opens a database using a [driver.Connector], allowing drivers to bypass a string based data source name. Most users will open a database via a driver-specific connection helper function that returns a [*DB]. No database drivers are included in the Go standard library. See https://golang.org/s/sqldrivers for a list of third-party drivers. OpenDB may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call [DB.Ping]. ...
database/sql/sql.go:832
func Register(name string, driver driver.Driver)
Register makes a database driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.
database/sql/sql.go:56
Types
type ColumnType struct {
// contains filtered or unexported fields
}ColumnType contains the name and type of a column.
database/sql/sql.go:3216
func (ci *ColumnType) DatabaseTypeName() string
DatabaseTypeName returns the database system name of the column type. If an empty string is returned, then the driver type name is not supported. Consult your driver documentation for a list of driver data types. [ColumnType.Length] specifiers are not included. Common type names include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", and "BIGINT".
database/sql/sql.go:3270
func (ci *ColumnType) DecimalSize() (int64, bool)
DecimalSize returns the scale and precision of a decimal type. If not applicable or if not supported ok is false.
database/sql/sql.go:3247
func (ci *ColumnType) Length() (int64, bool)
Length returns the column type length for variable length column types such as text and binary field types. If the type length is unbounded the value will be [math.MaxInt64] (any database limits will still apply). If the column type is not variable length, such as an int, or if not supported by the driver ok is false.
database/sql/sql.go:3241
func (ci *ColumnType) Name() string
Name returns the name or alias of the column.
database/sql/sql.go:3232
func (ci *ColumnType) Nullable() bool
Nullable reports whether the column may be null. If a driver does not support this property ok will be false.
database/sql/sql.go:3260
func (ci *ColumnType) ScanType() reflect.Type
ScanType returns a Go type suitable for scanning into using [Rows.Scan]. If a driver does not support this property ScanType will return the type of an empty interface.
database/sql/sql.go:3254
type Conn struct {
// contains filtered or unexported fields
}Conn represents a single database connection rather than a pool of database connections. Prefer running queries from [DB] unless there is a specific need for a continuous single database connection. A Conn must call [Conn.Close] to return the connection to the database pool and may do so concurrently with a running query. After a call to [Conn.Close], all operations on the connection fail with [ErrConnDone].
database/sql/sql.go:1976
func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)
BeginTx starts a transaction. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. [Tx.Commit] will return an error if the context provided to BeginTx is canceled. The provided [TxOptions] is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
database/sql/sql.go:2110
func (c *Conn) Close() error
Close returns the connection to the connection pool. All operations after a Close will return with [ErrConnDone]. Close is safe to call concurrently with other operations and will block until all other operations finish. It may be useful to first cancel any used context and then call close directly after.
database/sql/sql.go:2152
func (c *Conn) ExecContext(ctx context.Context, query string, args ...any) (Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
database/sql/sql.go:2022
func (c *Conn) PingContext(ctx context.Context) error
PingContext verifies the connection to the database is still alive.
database/sql/sql.go:2012
func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's [*Stmt.Close] method when the statement is no longer needed. The provided context is used for the preparation of the statement, not for the execution of the statement.
database/sql/sql.go:2059
func (c *Conn) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
database/sql/sql.go:2032
func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...any) *Row
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until the [*Row.Scan] method is called. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, the [*Row.Scan] scans the first selected row and discards the rest.
database/sql/sql.go:2046
func (c *Conn) Raw(f func(...)) error
Raw executes f exposing the underlying driver connection for the duration of f. The driverConn must not be used outside of f. Once f returns and err is not [driver.ErrBadConn], the [Conn] will continue to be usable until [Conn.Close] is called.
database/sql/sql.go:2072
type DB struct {
// contains filtered or unexported fields
}DB is a database handle representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines. The sql package creates and frees connections automatically; it also maintains a free pool of idle connections. If the database has a concept of per-connection state, such state can be reliably observed within a transaction ([Tx]) or connection ([Conn]). Once [DB.Begin] is called, the returned [Tx] is bound to a single connection. Once [Tx.Commit] or [Tx.Ro...
database/sql/sql.go:507
func (db *DB) Begin() (*Tx, error)
Begin starts a transaction. The default isolation level is dependent on the driver. Begin uses [context.Background] internally; to specify the context, use [DB.BeginTx].
database/sql/sql.go:1886
func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)
BeginTx starts a transaction. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. [Tx.Commit] will return an error if the context provided to BeginTx is canceled. The provided [TxOptions] is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
database/sql/sql.go:1869
func (db *DB) Close() error
Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish. It is rare to Close a [DB], as the [DB] handle is meant to be long-lived and shared between many goroutines.
database/sql/sql.go:926
func (db *DB) Conn(ctx context.Context) (*Conn, error)
Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session. Every Conn must be returned to the database pool after use by calling [Conn.Close].
database/sql/sql.go:1945
func (db *DB) Driver() driver.Driver
Driver returns the database's underlying driver.
database/sql/sql.go:1930
func (db *DB) Exec(query string, args ...any) (Result, error)
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query. Exec uses [context.Background] internally; to specify the context, use [DB.ExecContext].
database/sql/sql.go:1684
func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
database/sql/sql.go:1667
func (db *DB) Ping() error
Ping verifies a connection to the database is still alive, establishing a connection if necessary. Ping uses [context.Background] internally; to specify the context, use [DB.PingContext].
database/sql/sql.go:916
func (db *DB) PingContext(ctx context.Context) error
PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
database/sql/sql.go:895
func (db *DB) Prepare(query string) (*Stmt, error)
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's [*Stmt.Close] method when the statement is no longer needed. Prepare uses [context.Background] internally; to specify the context, use [DB.PrepareContext].
database/sql/sql.go:1614
func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's [*Stmt.Close] method when the statement is no longer needed. The provided context is used for the preparation of the statement, not for the execution of the statement.
database/sql/sql.go:1594
func (db *DB) Query(query string, args ...any) (*Rows, error)
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query. Query uses [context.Background] internally; to specify the context, use [DB.QueryContext].
database/sql/sql.go:1754
func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
database/sql/sql.go:1737
func (db *DB) QueryRow(query string, args ...any) *Row
QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until [Row]'s Scan method is called. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, [*Row.Scan] scans the first selected row and discards the rest. QueryRow uses [context.Background] internally; to specify the context, use [DB.QueryRowContext].
database/sql/sql.go:1855
func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *Row
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until [Row]'s Scan method is called. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, [*Row.Scan] scans the first selected row and discards the rest.
database/sql/sql.go:1841
func (db *DB) SetConnMaxIdleTime(d time.Duration)
SetConnMaxIdleTime sets the maximum amount of time a connection may be idle. Expired connections may be closed lazily before reuse. If d <= 0, connections are not closed due to a connection's idle time.
database/sql/sql.go:1069
func (db *DB) SetConnMaxLifetime(d time.Duration)
SetConnMaxLifetime sets the maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If d <= 0, connections are not closed due to a connection's age.
database/sql/sql.go:1047
func (db *DB) SetMaxIdleConns(n int)
SetMaxIdleConns sets the maximum number of connections in the idle connection pool. If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit. If n <= 0, no idle connections are retained. The default max idle connections is currently 2. This may change in a future release.
database/sql/sql.go:995
func (db *DB) SetMaxOpenConns(n int)
SetMaxOpenConns sets the maximum number of open connections to the database. If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).
database/sql/sql.go:1029
func (db *DB) Stats() DBStats
Stats returns database statistics.
database/sql/sql.go:1215
type DBStats struct {
MaxOpenConnections int // Maximum number of open connections to the database.
// Pool Status
OpenConnections int // The number of established connections both in use and i...DBStats contains database statistics.
database/sql/sql.go:1198
type IsolationLevel int
IsolationLevel is the transaction isolation level used in [TxOptions].
database/sql/sql.go:126
func (i IsolationLevel) String() string
String returns the name of the transaction isolation level.
database/sql/sql.go:144
type NamedArg struct {
// Name is the name of the parameter placeholder.
//
// If empty, the ordinal position in the argument list will be
// used.
//
// Name must omit any symbol prefix.
N...A NamedArg is a named argument. NamedArg values may be used as arguments to [DB.Query] or [DB.Exec] and bind to the corresponding named parameter in the SQL statement. For a more concise way to create NamedArg values, see the [Named] function.
database/sql/sql.go:88
type Null[T any] struct {
V T
Valid bool
}Null represents a value that may be null. Null implements the [Scanner] interface so it can be used as a scan destination: var s Null[string] err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s) ... if s.Valid { // use s.V } else { // NULL value } T should be one of the types accepted by [driver.Value].
database/sql/sql.go:415
func (n *interface{}) Scan(value any) errordatabase/sql/sql.go:420
func (n interface{}) Value() (driver.Value, error)database/sql/sql.go:429
type NullBool struct {
Bool bool
Valid bool // Valid is true if Bool is not NULL
}NullBool represents a bool that may be null. NullBool implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:352
func (n *NullBool) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:358
func (n NullBool) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:368
type NullByte struct {
Byte byte
Valid bool // Valid is true if Byte is not NULL
}NullByte represents a byte that may be null. NullByte implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:299
func (n *NullByte) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:305
func (n NullByte) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:316
type NullFloat64 struct {
Float64 float64
Valid bool // Valid is true if Float64 is not NULL
}NullFloat64 represents a float64 that may be null. NullFloat64 implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:326
func (n *NullFloat64) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:332
func (n NullFloat64) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:342
type NullInt16 struct {
Int16 int16
Valid bool // Valid is true if Int16 is not NULL
}NullInt16 represents an int16 that may be null. NullInt16 implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:272
func (n *NullInt16) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:278
func (n NullInt16) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:289
type NullInt32 struct {
Int32 int32
Valid bool // Valid is true if Int32 is not NULL
}NullInt32 represents an int32 that may be null. NullInt32 implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:246
func (n *NullInt32) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:252
func (n NullInt32) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:262
type NullInt64 struct {
Int64 int64
Valid bool // Valid is true if Int64 is not NULL
}NullInt64 represents an int64 that may be null. NullInt64 implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:220
func (n *NullInt64) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:226
func (n NullInt64) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:236
type NullString struct {
String string
Valid bool // Valid is true if String is not NULL
}NullString represents a string that may be null. NullString implements the [Scanner] interface so it can be used as a scan destination: var s NullString err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s) ... if s.Valid { // use s.String } else { // NULL value }
database/sql/sql.go:194
func (ns *NullString) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:200
func (ns NullString) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:210
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}NullTime represents a [time.Time] that may be null. NullTime implements the [Scanner] interface so it can be used as a scan destination, similar to [NullString].
database/sql/sql.go:378
func (n *NullTime) Scan(value any) error
Scan implements the [Scanner] interface.
database/sql/sql.go:384
func (n NullTime) Value() (driver.Value, error)
Value implements the [driver.Valuer] interface.
database/sql/sql.go:394
type Out struct {
// Dest is a pointer to the value that will be set to the result of the
// stored procedure's OUTPUT parameter.
Dest any
// In is whether the parameter is an INOUT parameter...Out may be used to retrieve OUTPUT value parameters from stored procedures. Not all drivers and databases support OUTPUT value parameters. Example usage: var outArg string _, err := db.ExecContext(ctx, "ProcName", sql.Named("Arg1", sql.Out{Dest: &outArg}))
database/sql/sql.go:477
type RawBytes []byte
RawBytes is a byte slice that holds a reference to memory owned by the database itself. After a [Rows.Scan] into a RawBytes, the slice is only valid until the next call to [Rows.Next], [Rows.Scan], or [Rows.Close].
database/sql/sql.go:180
type Result interface {
// LastInsertId returns the integer generated by the database
// in response to a command. Typically this will be from an
// "auto increment" column when inserting a new ...A Result summarizes an executed SQL command.
database/sql/sql.go:3532
type Row struct {
// contains filtered or unexported fields
}Row is the result of calling [DB.QueryRow] to select a single row.
database/sql/sql.go:3475
func (r *Row) Err() error
Err provides a way for wrapping packages to check for query errors without calling [Row.Scan]. Err returns the error, if any, that was encountered while running the query. If this error is not nil, this error will also be returned from [Row.Scan].
database/sql/sql.go:3527
func (r *Row) Scan(dest ...any) error
Scan copies the columns from the matched row into the values pointed at by dest. See the documentation on [Rows.Scan] for details. If more than one row matches the query, Scan uses the first row and discards the rest. If no row matches the query, Scan returns [ErrNoRows].
database/sql/sql.go:3486
type Rows struct {
// contains filtered or unexported fields
}Rows is the result of a query. Its cursor starts before the first row of the result set. Use [Rows.Next] to advance from row to row.
database/sql/sql.go:2929
func (rs *Rows) Close() error
Close closes the [Rows], preventing further enumeration. If [Rows.Next] is called and returns false and there are no further result sets, the [Rows] are closed automatically and it will suffice to check the result of [Rows.Err]. Close is idempotent and does not affect the result of [Rows.Err].
database/sql/sql.go:3433
func (rs *Rows) Columns() ([]string, error)
Columns returns the column names. Columns returns an error if the rows are closed.
database/sql/sql.go:3183
func (rs *Rows) ColumnTypes() ([]*ColumnType, error)
ColumnTypes returns column information such as column type, length, and nullable. Some information may not be available from some drivers.
database/sql/sql.go:3200
func (rs *Rows) Err() error
Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit [Rows.Close].
database/sql/sql.go:3136
func (rs *Rows) Next() bool
Next prepares the next result row for reading with the [Rows.Scan] method. It returns true on success, or false if there is no next result row or an error happened while preparing it. [Rows.Err] should be consulted to distinguish between the two cases. Every call to [Rows.Scan], even the first one, must be preceded by a call to [Rows.Next].
database/sql/sql.go:3029
func (rs *Rows) NextResultSet() bool
NextResultSet prepares the next result set for reading. It reports whether there is further result sets, or false if there is no further result set or if there is an error advancing to it. The [Rows.Err] method should be consulted to distinguish between the two cases. After calling NextResultSet, the [Rows.Next] method should always be called before scanning. If there are further result sets they may not have rows in the result set.
database/sql/sql.go:3095
func (rs *Rows) Scan(dest ...any) error
Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in [Rows]. Scan converts columns read from the database into the following common Go types and special types provided by the sql package: *string *[]byte *int, *int8, *int16, *int32, *int64 *uint, *uint8, *uint16, *uint32, *uint64 *bool *float32, *float64 *interface{} *RawBytes *Rows (cursor value) any type implementing Scanner (see ...
database/sql/sql.go:3365
type Scanner interface {
// Scan assigns a value from a database driver.
//
// The src value will be of one of the following types:
//
// int64
// float64
// bool
// []byte
// ...Scanner is an interface used by [Rows.Scan].
database/sql/sql.go:447
type Stmt struct {
// contains filtered or unexported fields
}Stmt is a prepared statement. A Stmt is safe for concurrent use by multiple goroutines. If a Stmt is prepared on a [Tx] or [Conn], it will be bound to a single underlying connection forever. If the [Tx] or [Conn] closes, the Stmt will become unusable and all operations will return an error. If a Stmt is prepared on a [DB], it will remain usable for the lifetime of the [DB]. When the Stmt needs to execute on a new underlying connection, it will prepare itself on the new connection automatically.
database/sql/sql.go:2603
func (s *Stmt) Close() error
Close closes the statement.
database/sql/sql.go:2884
func (s *Stmt) Exec(args ...any) (Result, error)
Exec executes a prepared statement with the given arguments and returns a [Result] summarizing the effect of the statement. Exec uses [context.Background] internally; to specify the context, use [Stmt.ExecContext].
database/sql/sql.go:2667
func (s *Stmt) ExecContext(ctx context.Context, args ...any) (Result, error)
ExecContext executes a prepared statement with the given arguments and returns a [Result] summarizing the effect of the statement.
database/sql/sql.go:2643
func (s *Stmt) Query(args ...any) (*Rows, error)
Query executes a prepared query statement with the given arguments and returns the query results as a *Rows. Query uses [context.Background] internally; to specify the context, use [Stmt.QueryContext].
database/sql/sql.go:2837
func (s *Stmt) QueryContext(ctx context.Context, args ...any) (*Rows, error)
QueryContext executes a prepared query statement with the given arguments and returns the query results as a [*Rows].
database/sql/sql.go:2785
func (s *Stmt) QueryRow(args ...any) *Row
QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned [*Row], which is always non-nil. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, the [*Row.Scan] scans the first selected row and discards the rest. Example usage: var name string err := nameByUseridStmt.QueryRow(id).Scan(&name) QueryRow uses [context.Background] int...
database/sql/sql.go:2879
func (s *Stmt) QueryRowContext(ctx context.Context, args ...any) *Row
QueryRowContext executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned [*Row], which is always non-nil. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, the [*Row.Scan] scans the first selected row and discards the rest.
database/sql/sql.go:2857
type Tx struct {
// contains filtered or unexported fields
}Tx is an in-progress database transaction. A transaction must end with a call to [Tx.Commit] or [Tx.Rollback]. After a call to [Tx.Commit] or [Tx.Rollback], all operations on the transaction fail with [ErrTxDone]. The statements prepared for a transaction by calling the transaction's [Tx.Prepare] or [Tx.Stmt] methods are closed by the call to [Tx.Commit] or [Tx.Rollback].
database/sql/sql.go:2166
func (tx *Tx) Commit() error
Commit commits the transaction.
database/sql/sql.go:2287
func (tx *Tx) Exec(query string, args ...any) (Result, error)
Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE. Exec uses [context.Background] internally; to specify the context, use [Tx.ExecContext].
database/sql/sql.go:2524
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (Result, error)
ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
database/sql/sql.go:2511
func (tx *Tx) Prepare(query string) (*Stmt, error)
Prepare creates a prepared statement for use within a transaction. The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back. To use an existing prepared statement on this transaction, see [Tx.Stmt]. Prepare uses [context.Background] internally; to specify the context, use [Tx.PrepareContext].
database/sql/sql.go:2397
func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)
PrepareContext creates a prepared statement for use within a transaction. The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back. To use an existing prepared statement on this transaction, see [Tx.Stmt]. The provided context will be used for the preparation of the context, not for the execution of the returned statement. The returned statement will run in the transaction context.
database/sql/sql.go:2372
func (tx *Tx) Query(query string, args ...any) (*Rows, error)
Query executes a query that returns rows, typically a SELECT. Query uses [context.Background] internally; to specify the context, use [Tx.QueryContext].
database/sql/sql.go:2542
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
QueryContext executes a query that returns rows, typically a SELECT.
database/sql/sql.go:2529
func (tx *Tx) QueryRow(query string, args ...any) *Row
QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until [Row]'s Scan method is called. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, the [*Row.Scan] scans the first selected row and discards the rest. QueryRow uses [context.Background] internally; to specify the context, use [Tx.QueryRowContext].
database/sql/sql.go:2566
func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until [Row]'s Scan method is called. If the query selects no rows, the [*Row.Scan] will return [ErrNoRows]. Otherwise, the [*Row.Scan] scans the first selected row and discards the rest.
database/sql/sql.go:2552
func (tx *Tx) Rollback() error
Rollback aborts the transaction.
database/sql/sql.go:2358
func (tx *Tx) Stmt(stmt *Stmt) *Stmt
Stmt returns a transaction-specific prepared statement from an existing statement. Example: updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?") ... tx, err := db.Begin() ... res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203) The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back. Stmt uses [context.Background] internally; to specify the context, use [Tx.StmtContext].
database/sql/sql.go:2505
func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt
StmtContext returns a transaction-specific prepared statement from an existing statement. Example: 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) The provided context is used for the preparation of the statement, not for the execution of the statement. The returned statement operates within the transaction and will be closed when the transaction has been co...
database/sql/sql.go:2417
type TxOptions struct {
// Isolation is the transaction isolation level.
// If zero, the driver or database's default level is used.
Isolation IsolationLevel
ReadOnly bool
}TxOptions holds the transaction options to be used in [DB.BeginTx].
database/sql/sql.go:170