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.
package main
import (
“database/sql”
“fmt”
_ "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?
From the documentation (emphasis added):
LastInsertId
The
database/sql
package provides a LastInsertId method to return the last inserted row's id. Oracle does not provide such functionality, but if you append... RETURNING col /*LastInsertId*/
to your SQL, then it will be presented as LastInsertId. Note that you have to mark with a/*LastInsertId*/
(case insensitive) yourRETURNING
part, to allow ora to return the last column asLastInsertId()
. That column must fit inint64
, though!