GaussDB-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") |
| (tx *Tx)StmtContext(ctx context.Context, stmt *Stmt) | 在给定上下文中,为已有的语句返回一个事务专用的预备语句。 | *Stmt |
#### 参数说明
| **参数** | **参数说明** |
| ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ctx | 表示给定的上下文。 |
| query | 被执行的sql语句。 |
| args | 被执行sql语句需要绑定的参数。支持按位置绑定和按名称绑定,详情见DB类型中的[示例](https://doc.hcs.huawei.com/db/zh-cn/gaussdbqlh/24.1.30/devg-dist/gaussdb-12-0237.html#ZH-CN_TOPIC_0000001911666245__zh-cn_topic_0000001704458049_section1862551335114)。 |
| stmt | 已有的预处理语句,一般指prepare语句返回的预处理语句。 |
NOTICE:
1. Query类接口Query()、QueryContext()、QueryRow()、QueryRowContext()通常用于查询语句,如SELECT语句。操作语句使用Exec()接口执行,若非查询语句通过Query类接口执行,则执行结果可能与预期不符,因此不建议使用Query类接口执行非查询语句,例如UPDATE/INSERT等。
1. 使用Query类接口执行查询语句的结果需要通过[type Rows](https://doc.hcs.huawei.com/db/zh-cn/gaussdbqlh/24.1.30/devg-dist/gaussdb-12-0240.html#ZH-CN_TOPIC_0000001911585197)中Next()接口获取,若不通过Next()接口获取,可能会产生不可预期的错误。
更多详情请参考GaussDB 文档中心:<https://doc.hcs.huawei.com/db/zh-cn/gaussdbqlh/24.1.30/productdesc/qlh_03_0001.html>