N
N
nurzhannogerbek2019-03-23 19:53:48
Oracle
nurzhannogerbek, 2019-03-23 19:53:48

How to use an array in a database query from Golang?

Hello comrades! Help please deal with the problem. Golang
application has an array. In my case it looks like this . I am trying to use this array in an Oracle database query. I am using goracle library for this purpose. My code looks like this:[27625 27626 27627]

args := make([]interface{}, len(ids))

for i, id := range ids {
    args[i] = id
}

stmt := `SELECT ORGANIZATION_ID, ORGANIZATION_NAME FROM ORG WHERE ORGANIZATION_ID IN (:value` + strings.Repeat(",:value", len(args)-1) + `)`

fmt.Println(stmt)
// return: SELECT ORGANIZATION_ID, ORGANIZATION_NAME FROM ORG WHERE ORGANIZATION_ID IN (:value,:value,:value)

fmt.Println(args)
// return: [27625 27626 27627]

rows, err := database.OracleDB.Query(stmt, args); if err != nil {
    utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
    return
}

When I try to query the database, I get the following error. How to fix it?
ERROR :
1. arg: unknown type []interface {}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dasha Tsiklauri, 2019-03-23
@nurzhannogerbek

looks like you need to pass a pointer to a string instead of a string
args[i] = &id

U
uvelichitel, 2019-03-23
@uvelichitel

The function expects multiple arguments of type , and you pass one type to it . There is syntactic sugar for passing arguments to a variadic functionQuery(query string, args ...interface{})interface{}[]interface{}

database.OracleDB.Query(stmt, args...)  //ellipses ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question