亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將圖像作為 BLOB 插入 Oracle 數據庫

如何將圖像作為 BLOB 插入 Oracle 數據庫

Go
有只小跳蛙 2023-07-31 16:28:01
我有一個帶有 A_LOB_TABLE 表的數據庫:我想使用 goracle 包將具有任何(假設為“1”)ID 的 BLOB 圖像插入到 A_LOB_TABLE 中。這是我的代碼:ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)defer cancel()// To have a valid LOB locator, we have to keep the Stmt around.qry := `DECLARE tmp BLOB;BEGIN  DBMS_LOB.createtemporary(tmp, TRUE, DBMS_LOB.SESSION);  :1 := tmp;END;`    tx, err := testDb.BeginTx(ctx, nil)    if err != nil {        fmt.Println(err)    }    defer tx.Rollback()stmt, err := tx.PrepareContext(ctx, qry)if err != nil {    fmt.Printf("%s: %w", qry, err)}defer stmt.Close()var tmp goracle.Lobif _, err := stmt.ExecContext(ctx, goracle.LobAsReader(), sql.Out{Dest: &tmp}); err != nil {    fmt.Printf("Failed to create temporary lob: %+v", err)}fmt.Printf("tmp: %#v", tmp)// Get file as bytes (it needs to be in the same dir as code is)dat, err := ioutil.ReadFile("./sample.png")if err != nil {    fmt.Println(".....Error Opening File")    fmt.Println(err)    return}if _, err := tx.ExecContext(ctx,    "BEGIN dbms_lob.append(:1, :2); END;",    tmp, goracle.Lob{Reader: bytes.NewReader(dat[:])},); err != nil {    fmt.Printf("Failed to write buffer(%v) to lob(%v): %+v", dat, tmp, err)}// INSERTING LOB - starting...._, err = testDb.Exec("insert into A_LOB_TABLE(id, image) VALUES(:1, :2)",  1, tmp)if err != nil {    fmt.Println(".....Error Inserting data - BLOB")    fmt.Println(err)    return}// INSERTING LOB - ended.但這不起作用。它在行輸出錯誤_, err = testDb.Exec("insert into A_LOB_TABLE(id, image) VALUES(:1, :2)",  1, tmp)說:dpiStmt_execute(mode=32 arrLen=-1): ORA-22922: LOB 值不存在
查看完整描述

1 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

經過數十個小時嘗試弄清楚如何使用 goracle Go 驅動程序將 BLOB 插入到 OracleDB 中,我得到了答案。


長話短說


完整代碼(顯然根據您的需要調整數據庫連接和文件位置&&文件名等變量):


package main


import (

    "bytes"

    "context"

    "database/sql"

    "fmt"

    "io/ioutil"

    "time"

goracle "gopkg.in/goracle.v2"

)


func main() {

    fmt.Println("... Setting up Database Connection")

    testDb, err := sql.Open("goracle",

        "sys" +

            "/" +

            "Oracle18" +

            "@" +

            "localhost:32118" +

            "/" +

            "XE as sysdba")

    if err != nil {

        fmt.Println("... DB Setup Failed")

        fmt.Println(err)

        return

    }

    defer testDb.Close()


// Get file as bytes

dat, err := ioutil.ReadFile("./sample.png")

if err != nil {

    fmt.Println(".....Error Opening File")

    fmt.Println(err)

    return

}


// BLOB creation && passing file into it

directLob := goracle.Lob{ Reader: bytes.NewReader(dat[:])}


fmt.Println("\nINSERTING BLOB - starting....")

// Working BLOB insertion - INSERT COMMAND

_, err = testDb.Exec("insert into AA_LIVE_FRAMES(id, frame) VALUES(:1, :2)",  1, directLob)

if err != nil {

    fmt.Println(".....Error Inserting BLOB")

    fmt.Println(err)

    return

}

fmt.Println("\nINSERTING BLOB - ended.")


fmt.Println("\nSuccess")

}

這是正在運行的 main.go 文件。


較長版本的答案


如果代碼如此簡單,為什么我花了這么長時間?


當我開始解決這個問題時,我在 goracle 文檔中沒有找到任何說明如何將 BLOB 插入 OracleDB 的教程或示例。當然,這個 goracle.Lob 結構具有很好的功能 - Read()。這是我第一次嘗試,聲明這個 goracle.Lob 并使用它的 Read() 函數,但它給了我錯誤 - nil 地址等等。我不知道為什么,嘗試調試它 - 沒有結果。然后我更深入,我從 goracle 包中提取了一些測試代碼,展示了如何創建臨時 LOB - 其中一些代碼顯示在我的問題答案中。再次,沒有成功。然后,我嘗試原始 SQL,再次失?。ㄎ艺J為因為我的 OracleDB 是 docker - 創建目錄,將 BLOB 插入 OracleDB 所需的目錄不起作用;nvm)。


最后,我不知道為什么,我嘗試過結構文字:


directLob := goracle.Lob{ Reader: bytes.NewReader(dat[:])}

并工作了。


好吧,為什么我要輸入這些內容?因為,據我所知,這樣做:


directLob := goracle.Lob{ Reader: bytes.NewReader(dat[:])}

和這個:


directLob := goracle.Lob{}

directLob.Read(dat[:])

應該是一樣的嗎?或者我完全錯了?


查看完整回答
反對 回復 2023-07-31
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號