LastInsertId is returning 0, when using database/sql package with oracle driver

Hi,

I am using database/sql package with oracle driver (“gopkg.in/rana/ora.v4”), when I am inserting the data, it’s LastInsertId Method is returning 0, while data is successfully inserted.
Attaching code.

import (
“database/sql”
“fmt”
“os”
“strconv”
“strings”
“sync”

_ "gopkg.in/rana/ora.v4"

)

func main() {
conn, err = sql.Open(“ora”, username+"/"+password+"@"+host+":"+port+"/"+sid)
query := “INSERT INTO Table (C2) VALUES (:C2)”
result, err := conn.Exec(query, “Test”)
if err!= nil {
panic(err)
}
lastId := result.LastInsertId() // returning 0
fmt.Println(lastId)
}

Please tell me why this is happening?

result.LastInsertId() should also return an error as a second value, please check if it is non-nil. Not all databases support this function.

As you do not use the second value in the code pasted, I’m not even sure if it compiles at all, and I can not test as I’m in a mobile.

Edit

Please also read https://github.com/rana/ora#lastinsertid

1 Like

It is bette idea to use an SP and in this SP return the last id

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