type Stmt
type Stmt接口主要用于预处理SQL语句,常用方法如表1所示。
| 方法 | 描述 | 返回值类型 |
| (s *Stmt)Close() | 关闭指定的预处理语句。 | error |
| (s *Stmt)Exec(args ...interface{}) | 执行不返回数据行的预处理语句。支持PBE特性。PBE(Prepare-Bind-Execute)是一种发送和执行查询的方式,DN可通过复杂查询协议接收PBE报文执行语句。 说明:
| Result, error |
| (s *Stmt)ExecContext(ctx context.Context, args ...interface{}) | 在给定的上下文中执行不返回数据行的预处理语句。 | Result, error |
| (s *Stmt)Query(args ...interface{}) | 执行预处理语句,以*Rows形式返回查询结果。 | *Rows, error |
| (s *Stmt)QueryContext(ctx context.Context, args ...interface{}) | 在给定的上下文中执行预处理语句,以*Rows形式返回查询结果。 | *Rows, error |
| (s *Stmt)QueryRow(args ...interface{}) | 执行预处理语句,以*Row形式返回查询结果。 | *Row |
| (s *Stmt)QueryRowContext (ctx context.Context, args ...interface{}) | 在给定的上下文中执行预处理语句,以*Row形式返回查询结果。 | *Row |
参数说明
| 参数 | 参数说明 |
| ctx | 表示给定的上下文。 |
| args | 待执行SQL语句需要绑定的参数。支持按位置绑定和按名称绑定,参见示例。 |
- Query类方法Query()、QueryContext()、QueryRow()、QueryRowContext()用于执行返回数据行的语句。
- Exec类方法Exec()、ExecContext()用于执行不返回数据行的语句。
- 若使用Query类方法执行DML操作,则可能出现执行结果与预期不符的情况。因此不建议使用Query类方法执行增、删、改操作。