oracle - 当使用带有 oracle 驱动程序的数据库/sql 包时,LastInsertId 返回 0

标签 oracle go last-insert-id lastinsertid

我正在使用带有 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) your RETURNING part, to allow ora to return the last column as LastInsertId(). That column must fit in int64, though!

关于oracle - 当使用带有 oracle 驱动程序的数据库/sql 包时,LastInsertId 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055461/

相关文章:

php - 将最后一个 id 插入的变量发送到 php 中的另一个页面

SQL错误: ORA-01861: literal does not match format string 01861

go - 扇入 channel 至单 channel

php - 当表先前被锁定时是否可以使用 PDO lastInsertId() ?

go - 在 Golang 中过滤字节流的正确方法?

go - 检索后无法将数据插入数据库

java - 从 MySQL 获取最后一个 id

当系统处于 TimeZone C 时,SQL 将 TimeStamp 列的 TimeZone 从 A 转换为 B

sql - ORA-00907 : Missing Right Parenthesis On Creating Foreign Key Oracle 12c

sql - 在结果集中并排连接列