我有一个带有 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.Lob
if _, 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 值不存在
慕尼黑的夜晚无繁华
相关分类