通过 sql 脚本插入 BLOB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2607326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:57:27  来源:igfitidea点击:

insert a BLOB via a sql script?

sqlscriptingblobh2

提问by dm76

I have an H2 database (http://www.h2database.com) and I'd like to insert a file into a BLOB field via a plain simple sql script (to populate a test database for instance). I know how to do that via the code but I cannot find how to do the sql script itself.

我有一个 H2 数据库(http://www.h2database.com),我想通过一个简单的 sql 脚本(例如填充测试数据库)将文件插入到 BLOB 字段中。我知道如何通过代码做到这一点,但我找不到如何执行 sql 脚本本身。

I tried to pass the path , i.e.

我试图通过路径,即

INSERT INTO mytable (id,name,file) VALUES(1,'file.xml',/my/local/path/file.xml);

but this fails.

但这失败了。

Within the code (java for instance), it's easy to create a File object and pass that in, but directly from a sql script, I'm stuck...

在代码中(例如 java),很容易创建一个 File 对象并将其传入,但是直接从 sql 脚本中,我被卡住了...

Any idea ?

任何的想法 ?

David

大卫

回答by trashgod

For testing, you can insert literal hex bytesor use the RAWTOHEX(string)function, as shown below.

为了测试,您可以插入文字十六进制字节或使用该RAWTOHEX(string)函数,如下所示。

create table a(id integer, item blob);
insert into a values(1,'54455354');
insert into a values(2, RAWTOHEX('Test'));
select UTF8TOSTRING(item) from a;
TEST
Test

Addendum: For loading BLOBfields from a file, FILE_READ(fileNameString)may be a useful alternative.

附录:对于BLOB从文件加载字段,FILE_READ(fileNameString)可能是一个有用的替代方法。

insert into a values(3, FILE_READ('file.dat'));