Updated on 2024-05-07 GMT+08:00

type Stmt

The following table describes type Stmt.

Method

Description

Return Value

(s *Stmt)Close()

Closes a specified prepared statement.

error

(s *Stmt)Exec(args ...interface{})

Executes a prepared statement with specified parameters and returns a Result value. The Prepare-Bind-Execute (PBE) feature is supported. PBE is a method of sending and executing queries. The CN can receive PBE packets through complex query protocols to execute statements.
NOTE:
  1. The placeholder of a precompiled statement can be a dollar sign ($) or a question mark (?).
  2. The number of placeholders in a precompiled statement is determined by the database. When the number of table columns exceeds the database limit or does not match the number of current table columns, the server returns an error.
  3. Batch PBE processing supports addition, deletion, and modification. During the batch operation, the maximum length of a U packet is limited to 1 GB minus 1 byte, that is, 0x3fffffff bytes. If the length exceeds the limit, "bind message length XXX too long. This can be caused by very large or incorrect ength specifications on InputStream parameters" will be reported.
  4. When a record is inserted, the performance of PBE deteriorates greatly compared with that of a single-query statement (conn.simpleExec). Therefore, you are advised to use single-query statements, rather than the PBE statement.
  5. After the underlying error processing of the driver is reconstructed, the PBE performance decreases by less than 5%.

Result and error

(s *Stmt)ExecContext(ctx context.Context,

args ...interface{})

Executes a prepared statement with specified parameters in a specified context and returns a Result value.

Result and error

(s *Stmt)Query(args ...interface{})

Executes a prepared statement with specified parameters and returns *Rows as the query result.

*Rows and error

(s *Stmt)QueryContext(ctx context.Context,

args ...interface{})

Executes a prepared statement with specified parameters in a specified context and returns *Rows as the query result.

*Rows and error

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

Executes a prepared statement with specified parameters and returns *Row as the result.

*Row

(s *Stmt)QueryRowContext (ctx context.Context,

args ...interface{})

Executes a prepared statement with specified parameters in a specified context and returns *Row as the result.

*Row

Parameters

Parameter

Description

ctx

Specified context.

query

Executed SQL statement.

args

Parameter that needs to be bound to the executed SQL statement. Binding by location and binding by name are supported. For details, see Examples in section "type DB."

  1. The Query(), QueryContext(), QueryRow(), and QueryRowContext() APIs are usually used in query statements, such as SELECT. The Exec() API is used for executing operation statements. If query APIs are used to execute non-query statements, the execution result may be unexpected. Therefore, you are advised not to use the query APIs to execute non-query statements, such as UPDATE and INSERT.
  2. The result of executing a query statement using a query API needs to be obtained through the Next() API in type Rows. If the result is not obtained through the Next() API, unexpected errors may occur.