type Tx
type Tx如下表所示。
方法 | 描述 | 返回值 |
(tx *Tx)Commit() | 提交事务。 | error |
(tx *Tx)Exec(query string, args ...interface{}) | 执行一个不返回数据行的操作。 | Result, error |
(tx *Tx)ExecContext(ctx context.Context, query string, args ...interface{}) | 在给定上下文中,执行一个不返回数据行的操作。 | Result, error |
(tx *Tx)Prepare(query string) | 为以后的查询或执行创建一个预备语句。返回的语句将在事务中执行,并且一旦事务被提交或回滚就不能再使用。 | *Stmt, error |
(tx *Tx)PrepareContext(ctx context.Context, query string) | 为以后的查询或执行创建一个预备语句。返回的语句将在事务中执行,并且一旦事务被提交或回滚就不能再使用。 给定的上下文将用于预备阶段,而不是事务执行阶段。该方法返回的语句将在事务上下文中执行。 | *Stmt, error |
(tx *Tx)Query(query string, args ...interface{}) | 执行一个返回数据行的查询。 | *Rows, error |
(tx *Tx)QueryContext(ctx context.Context, query string, args ...interface{}) | 在给定上下文中,执行一个返回数据行的查询。 | *Rows, error |
(tx *Tx)QueryRow(query string, args ...interface{}) | 执行一个只返回一个数据行的查询。 | *Row |
(tx *Tx)QueryRowContext(ctx context.Context, query string, args ...interface{}) | 在给定上下文中,执行一个只返回一个数据行的查询。 | *Row |
(tx *Tx) Rollback() | 事务回滚。 | error |
(tx *Tx)Stmt(stmt *Stmt) | 为已有的语句返回一个事务专用的预备语句。 示例: str, err := db.Prepare("insert into t1 values(:1, :2)")
tx, err := db.Begin()
res, err := tx.Stmt(str).Exec(1, "aaa") | *Stmt |
(tx *Tx)StmtContext(ctx context.Context, stmt *Stmt) | 在给定上下文中,为已有的语句返回一个事务专用的预备语句。 | *Stmt |
参数说明
参数 | 参数说明 |
ctx | 表示给定的上下文。 |
query | 被执行的sql语句。 |
args | 被执行sql语句需要绑定的参数。支持按位置绑定和按名称绑定,详情DB类型中的示例。 |
stmt | 已有的预处理语句,一般指prepare语句返回的预处理语句。 |

