我正在使用带有 oracle 驱动程序(“gopkg.in/rana/ora.v4”)的 database/sql 包,当我插入数据时,它的 LastInsertId 方法返回 0,而数据已成功插入。 附加代码。
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)
}
请告诉我为什么会这样?
最佳答案
来自 the documentation (强调):
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!
关于oracle - 当使用带有 oracle 驱动程序的数据库/sql 包时,LastInsertId 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055461/