简体   繁体   中英

Oracle DB SQL insert base64 string (> 4000 chars) into BLOB column of a table in SQL

I'm trying to insert a long base64 string (picture) in a BLOB column of a table and I get the error "string literal too long" cause The string literal is longer than 4000 characters and it's true but I don't know another way to do it, I'm using SQL developer.

My syntax is this one :

Insert into TABLE_NAME ( BLOB_COLUMN ) VALUES ('/9j/4AAQSkZJRgABAgA...2Q==');

I tried the functions CAST('Expression' AS BLOB), TO_BLOB but i get each time the same result.

How can I do it? I'm lost. Someone for help?

Thanks a lot

I have another suggestion because I tried all of these stored procedure methods that I found around Stackoverflow and just kept running into issues.

Use Oracle SQL Developer software. In that software you can navigate the table by double clicking it in the left pane:

双击左窗格中的表

Now the Table information will show in the main panel with different tabs. One of these tabs is called 'Data'.

打开“数据”标签

Now you can click the 'Insert Row (Ctrl + I)' button. This will allow you to manually upload your picture and let Oracle SQL Developer do the insert for you.

点击“插入行” 双击要添加图片的Blob列 点击“本地数据”下的“加载”

If the requirement is just to store a string, I would recommend CLOB instead of BLOB, as to store text in BLOB column, it has to be cast to RAW. The retrieval is equally complicated.

It can't be done in SQL, as 4000 characters is the maximum you can assign to a string field. However, here is my workaround using a small Pl/Sql Block (In Pl/Sql, maximum of 32767 characters.

CREATE TABLE clob_ins_test (
    clob_column CLOB
);
/

Table CLOB_INS_TEST created.

SET SERVEROUTPUT ON;
DECLARE
    v_long_text VARCHAR2( 5000 );
BEGIN
    v_long_text := LPAD( '1', 3500, '1' ) || LPAD( '1', 600, '1' ); /* Assign your string value here as v_long_text := '/9j/4AAQSkZJRgABAgA...2Q=='*/
    dbms_output.put_line( 'Length of String is ' || length( v_long_text ) ); -- Printing the length of the String assigned to v_long_text.
    INSERT INTO clob_ins_test VALUES ( v_long_text );
END;
/

Length of String is 4100

PL/SQL procedure successfully completed.

SELECT dbms_lob.getlength( clob_column ) Length, clob_column FROM CLOB_INS_TEST;

    LENGTH CLOB_COLUMN                                                                     
---------- --------------------------------------------------------------------------------
    4100 11111111111111111111111111111111111111111111111111111111111111111111111111111111

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM