我正在使用带有 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!


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!