Updated on 2025-02-27 GMT+08:00

type DB

The following table describes type DB.

Method

Description

Return Value

(db *DB)Begin()

Starts a transaction. The isolation level of the transaction is determined by the driver.

*Tx and error

(db *DB)BeginTx(ctx context.Context,

opts *TxOptions)

Starts a transaction with a specified transaction isolation level. A specified context is used until the transaction is committed or rolled back. If the context is canceled, the SQL package rolls back the transaction.

*Tx and error

(db *DB)Close()

Closes the database and releases all the opened resources.

error

(db *DB)Exec(query string,

args ...interface{})

Performs an operation that does not return rows of data.

Result and error

(db *DB)ExecContext(ctx context.Context,

query string, args ...interface{})

Performs an operation that does not return rows of data in a specified context.

Result and error

(db *DB)Ping()

Checks whether the database connection is still valid and establishes a connection if necessary.

error

(db *DB)PingContext(ctx context.Context)

Checks whether the database connection is still valid in a specified context and establishes a connection if necessary.

error

(db *DB)Prepare(query string)

Creates a prepared statement for subsequent queries or executions.

*Stmt and error

(db *DB)PrepareContext(ctx context.Context, query string)

Creates a prepared statement for subsequent queries or executions in a specified context.

*Stmt and error

(db *DB)Query(query string,

args ...interface{})

Executes a query and returns multiple rows of data.

*Rows and error

(db *DB)QueryContext(ctx context.Context,

query string, args ...interface{})

Executes a query and returns multiple rows of data in a specified context.

*Rows and error

(db *DB)QueryRow(query string,

args ...interface{})

Executes a query that returns only one row of data.

*Row

(db *DB)QueryRowContext(ctx context.Context,

query string, args ...interface{})

Executes a query that returns only one row of data in a specified context.

*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 the following example.

opts

Transaction isolation level and transaction access mode. The value of the transaction isolation level (opts.Isolation) can be sql.LevelReadUncommitted, sql.LevelReadCommitted, sql.LevelRepeatableRead, or sql.LevelSerializable. The value of the transaction access mode (opts.ReadOnly) can be true (read-only) or false (read/write).

Example:

// In this example, the username and password are stored in environment variables. Before running this example, set environment variables in the local environment (set the environment variable names based on the actual situation).
func main() {
 hostip := os.Getenv("GOHOSTIP")   // GOHOSTIP indicates the IP address written into the environment variable.
 port := os.Getenv("GOPORT")       // GOPORT indicates the port number written into the environment variable.
 usrname := os.Getenv("GOUSRNAME") // GOUSRNAME indicates the username for writing environment variables.
 passwd := os.Getenv("GOPASSWD")   // GOPASSWDW indicates the user password written into the environment variable.
 str := "host=" + hostip + " port=" + port + " user=" + usrname + " password=" + passwd + " dbname=postgres sslmode=disable"
 db, err := sql.Open("opengauss", str)
 if err != nil {
  log.Fatal(err)
 }
 defer db.Close()

 err = db.Ping()
 if err != nil {
  log.Fatal(err)
 }

	// Binding by location
 _, err = db.Exec("insert into test(id, name) values(:1, :2)", 1, "Zhang San")
 if err != nil {
  log.Fatal(err)
 }

	// Binding by name
 _, err = db.Exec("insert into test(id, name) values(:id, :name)", sql.Named("id", 1), sql.Named("name", "Zhang San"))
 if err != nil {
  log.Fatal(err)
 }
}