简体   繁体   中英

Insert BLOB in Derby Database using SQL

Currently I'm trying to import an SQL-INSERT dump from a postgres database into my Derby development/testing database using the Eclipse's Data Tools SQL scratchpad. The export created a lot of data that looked like the following:

CREATE TABLE mytable ( testfield BLOB );
INSERT INTO mytable ( testfield ) VALUES ('\x0123456789ABCDEF');

Executing it in Eclispe's SQL Scratchpad results in (translated from german):

Columns of type 'BLOB' shall not contain values of type 'CHAR'.

The problem seems, that the PostgreSQL admin tool exported BLOB data in a format like '\\x0123456789ABCDEF' which is not recognized by Derby (Embedded).

Changing this to X'0123456789ABCDEF' or simply '0123456789ABCDEF' did also not work.

The only thing that worked was CAST (X'123456789ABCDEF' AS BLOB) , but I'm not yet sure, if this results in the correct binary data when read back in Java and if the X'0123456789ABCDEF' is 100% portable.

CAST (...whatever... AS BLOB) doesn't work in java DB / Apache DERBY!

One must use the built-in system procedure SYSCS_UTIL.SYSCS_IMPORT_DATA_LOBS_FROM_EXTFILE . I do not think there is any other way. For instance:

CALL SYSCS_UTIL.SYSCS_IMPORT_DATA_LOBS_FROM_EXTFILE (
  'MYSCHEMA', 'MYTABLE', 
  'MY_KEY, MY_VARCHAR, MY_INT, MY_BLOB_DATA', 
  '1,3,4,2',
  'c:\tmp\import.txt', ',' , '"' , 'UTF-8',  
  0);

where the referenced "import.txt" file will be CSV like (as specified by the ',' and '"' arguments above) and contain as 2nd field (I scrambled the CSV field order versus DB column names order on purpose to illustrate) a file name that contains the binary data in proper for the BLOB's. For instance, "import.txt" is like:

"A001","c:\\tmp\\blobimport.dat.0.55/","TEST",123

where the supplied BLOB data file name bears the convention " filepath.offset.length/ "

Actually, you can first export your table with

CALL SYSCS_UTIL.SYSCS_EXPORT_TABLE_LOBS_TO_EXTFILE(
 'MYSCHEMA', 'MYTABLE', 'c:\tmp\export.txt', ',' ,'"', 
 'UTF-8', 'c:\tmp\blobexport.dat'); 

to generate sample files with the syntax to reuse on import.

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