【问题标题】:LastInsertId is returning 0, when using database/sql package with oracle driver当使用带有 oracle 驱动程序的 database/sql 包时,LastInsertId 返回 0
【发布时间】:2018-08-28 10:17:13
【问题描述】:

我正在使用带有oracle驱动程序的database/sql包(“gopkg.in/rana/ora.v4”),当我插入数据时,它的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)
}

请告诉我为什么会这样?

【问题讨论】:

    标签: oracle go last-insert-id lastinsertid


    【解决方案1】:

    来自the documentation(已添加重点):

    LastInsertId

    database/sql 包提供了一个 LastInsertId 方法来返回最后插入的行的 id。 Oracle 不提供此类功能,但如果您将... RETURNING col /*LastInsertId*/ 附加到您的SQL,那么它将显示为LastInsertId。 请注意,您必须用/*LastInsertId*/(不区分大小写)标记您的@ 987654325@ 部分,允许 ora 返回最后一列为LastInsertId()。不过,该列必须适合 int64

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多