用pl sql将blob数据插入oracle数据库

问题描述 投票:0回答:1

我在 oracle 数据库中创建了下表,我使用 pl sql 函数对其进行插入、更新和读取。

特别是,我在插入附件数据时遇到问题,即使用 blob 类型数据。此数据可以是图像或 pdf 文件。

这是桌子:

  CREATE TABLE "CID"."NOTIFICATIONS" 
   (    "ID" NUMBER(19,0) NOT NULL ENABLE, 
    "AGE_FROM" NUMBER(10,0), 
    "AGE_TO" NUMBER(10,0), 
    "DEPARTMENT_ID" NUMBER(10,0), 
    "LOCALITY_ID" NUMBER(10,0), 
    "MESSAGE_TITLE" VARCHAR2(4000 BYTE) NOT NULL ENABLE, 
    "MESSAGE_BODY" VARCHAR2(4000 BYTE), 
    "ATTACHMENT_TYPE" VARCHAR2(255 BYTE) DEFAULT 'none' NOT NULL ENABLE, 
    "ATTACHMENT" BLOB, 
    "CREATED_AT" TIMESTAMP (6), 
    "UPDATED_AT" TIMESTAMP (6), 
    "DELETED_AT" TIMESTAMP (6), 
     CHECK (attachment_type in ('none', 'img', 'pdf')) ENABLE, 
     CONSTRAINT "NOTIFICATIONS_ID_PK" PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "DATA"  ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "DATA" 
 LOB ("ATTACHMENT") STORE AS BASICFILE (
  TABLESPACE "DATA" ENABLE STORAGE IN ROW CHUNK 8192 RETENTION 
  NOCACHE LOGGING 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)) ;

这是将数据插入同一个表的 pl sql 函数

FUNCTION INSERT_ROW (p_table_name IN VARCHAR2, p_columns IN VARCHAR2, p_values IN VARCHAR2)
RETURN BOOLEAN
AS
BEGIN
  EXECUTE IMMEDIATE 'INSERT INTO ' || p_table_name || ' (' || p_columns || ') VALUES (' || p_values || ')';
  RETURN TRUE;
EXCEPTION
  WHEN OTHERS THEN
    RETURN FALSE;
END;

END CIUD_UTILIDADES_PKG;

这是要插入数据的列名:

AGE_FROM, AGE_TO, DEPARTMENT_ID, LOCALITY_ID, MESSAGE_TITLE, MESSAGE_BODY, ATTACHMENT_TYPE,ATTACHMENT,CREATED_AT

这是数据:

3,40,0,0,'First notification','First body notification','img','data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQYAAABbCAIAAAAeMLz2AAAAA3NCSVQICAjb4U/gAAAAEHRFWHRTb2Z0d2FyZQBTaHV0dGVyY4LQCQAAIABJREFUeNrsvWmvZNeVJbbWPufeiDfP+d7LOTkkZ1ISRVKiKFEiRQ01tmpA2d3lRrtsA22g/cGGAX+w/......, ,sysdate

我插入的附件字段错了吗?在这种情况下,它是一个 base64,但它是否以与其他字段相同的方式插入?

要在 Laravel 中插入数据,我所做的如下:

public function insertRow(string $table_name, string $columns, string $values): bool{

    $res = DB::statement("DECLARE l_result BOOLEAN; BEGIN l_result := CID_UTIL_PKG.INSERT_ROW(:table_name, :columns, :values); END;",
        [
            'table_name' => $table_name,
            'columns' => $columns,
            'values' => $values,
        ]);
    
    return $res;

}
sql oracle blob
1个回答
0
投票

您应该将文件读入 BLOB。为此,您需要Directory 对象 定义存储文件的文件夹。 下面是将文件读入 BLOB 的代码。参数是目录对象的名称和文件的名称。返回的 BLOB 可以插入到表中的 BLOB 列中。

FUNCTION FILE2BLOB  (mDir       IN  VARCHAR2,
                     mFileName  IN  VARCHAR2) RETURN BLOB
IS
BEGIN
    Declare 
        mBLOB       BLOB := Empty_Blob();
        mBinFile    BFILE := BFILENAME(mDir, mFileName);
    Begin
        DBMS_LOB.OPEN(mBinFile, DBMS_LOB.LOB_READONLY);             -- Open BFILE  
        DBMS_LOB.CreateTemporary(mBLOB, TRUE, DBMS_LOB.Session);    -- Initialyze BLOB locator
        DBMS_LOB.OPEN(mBLOB, DBMS_LOB.LOB_READWRITE);               -- Open BLOB locator for writing
        DBMS_LOB.LoadFromFile(mBLOB, mBinFile, DBMS_LOB.getLength(mBinFile));   -- Read BFILE into BLOB
        DBMS_LOB.CLOSE(mBLOB);              -- Close BLOB locator
        DBMS_LOB.CLOSE(mBinFile);           -- Close BFILE 
        RETURN mBLOB;       -- Return BLOB
    End;
END FILE2BLOB;

插入:

Insert Into YOUR_TABLE_NAME ( COL_1, COL_2, COL_BLOB )
Values( val_for_col_1, val_for_col_2, FILE2BLOB(your_DirectoryObject, yourFile) )
© www.soinside.com 2019 - 2024. All rights reserved.