Is there any way to Access the values in the Statement

package main

import (
    "database/sql"
    "fmt"
  _ "vNext/go_ibm_db"
)

func main() {
    con := "HOSTNAME=localhost;PORT=50000;DATABASE=dbname;UID=uname;PWD=password"
    db, err := sql.Open("go_ibm_db", con)
    if err != nil {
        fmt.Println(err)
    }
    db.Exec("Create table dbtype2(a int,c float)")
    st,err:=db.Prepare("Insert into dbtype2(a,c) values(?,?)")
	if err != nil{
	fmt.Println(err)
	}
	st.Query(12,3.45)
}

I want to access a variable(statement handle) in st. But when I tried to access it, it says that the variable is not exported. Is there any other way to access it.

Thanks.

db doesn’t even have a method Execute according to my personal type inference brain-algorythm…

Therefore I assume its a typo and you actually meant Exec.

Anyway… What do you mean by “in st”?

st is a variable of type database/sql/Result… Thats an interface, so it has no fields, but only two methods. If though you have implemented the concrete type, then you can simply access its fields in the implementation, independently if its exported or not.

Sorry It’s

func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
return db.ExecContext(context.Background(), query, args...)
}

Exec function in database/sql

type Stmt struct {
	// Immutable:
	db        *DB    // where we came from
	query     string // that created the Stmt
	stickyErr error  // if non-nil, this error is returned for all operations
	closemu sync.RWMutex // held exclusively during close, for read otherwise.
	cg   stmtConnGrabber
	cgds *driverStmt
	parentStmt *Stmt
	mu     sync.Mutex // protects the rest of the fields
	closed bool
	css []connStmt
	lastNumClosed uint64
}

st is an Stmt object which is returned by db.Prepare()
Is there any way to access the variables in that which are not exported.

No.

When we call db.Prepare() It internally creates a statement handle and then runs the SQL api’s. Is there any way to get that handle to the application.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.