简体   繁体   English

使用SQL在Derby数据库中插入BLOB

[英]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. 目前,我正在尝试使用Eclipse的数据工具SQL暂存器从Postgres数据库将SQL-INSERT转储导入我的Derby开发/测试数据库。 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): 在Eclispe的SQL Scratchpad中执行它会导致(从德语翻译):

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). 问题似乎出在PostgreSQL管理工具以'\\x0123456789ABCDEF'类的格式导出了BLOB数据,而Derby(Embedded)无法识别该格式。

Changing this to X'0123456789ABCDEF' or simply '0123456789ABCDEF' did also not work. 将其更改为X'0123456789ABCDEF'或仅将其更改为'0123456789ABCDEF'也不起作用。

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 (X'123456789ABCDEF' AS BLOB) ,但我不确定,如果用Java读回时这是否导致正确的二进制数据,以及X'0123456789ABCDEF'是否100%可移植。

CAST (...whatever... AS BLOB) doesn't work in java DB / Apache DERBY! CAST(...无论如何... AS BLOB)在Java DB / Apache DERBY中不起作用!

One must use the built-in system procedure SYSCS_UTIL.SYSCS_IMPORT_DATA_LOBS_FROM_EXTFILE . 必须使用内置系统过程 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: 其中所引用的“ import.txt”文件将是CSV之类的CSV文件(如上面的','和'“'参数所指定),并包含第二个字段(我为说明目的,对CSV字段顺序与DB列名称顺序进行了加扰)包含适合BLOB的二进制数据的文件名,例如,“ import.txt”类似于:

"A001","c:\\tmp\\blobimport.dat.0.55/","TEST",123 “ A001”,“ c:\\ tmp \\ blobimport.dat.0.55 /”,“测试”,123

where the supplied BLOB data file name bears the convention " filepath.offset.length/ " 提供的BLOB数据文件名遵循约定“ 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. 生成示例文件,其语法可在导入时重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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