简体   繁体   English

将BLOB从文件插入到SQL脚本中以嵌入H2数据库

[英]Insert BLOB from a file into a sql script to embed H2 database

I'm creating a SQL script to create a new schema and insert some values to an embed H2 database for use with integration tests in a Spring Boot application. 我正在创建一个SQL脚本来创建一个新的架构,并将一些值插入到嵌入式H2数据库中,以与Spring Boot应用程序中的集成测试一起使用。 One of the values I need to insert is a BLOB field on the sql table. 我需要插入的值之一是sql表上的BLOB字段。

I've succesfully used the FILE_READ function as described here . 我已经成功地使用了FILE_READ所描述的功能在这里

INSERT INTO MY_TABLE(ID, NAME, LOGO) 
VALUES('1', 'test1', FILE_READ('C:/myproject/logo.png'));

That function works well with full paths but I'm not been able to do that with relative paths. 该功能在完整路径下效果很好,但是我无法在相对路径下做到这一点。 That doesn't work well when the sources are downloaded and compiled (plus testing) in any other machine than mine. 当在除我的以外的任何其他机器上下载和编译源代码(以及进行测试)时,这将无法正常工作。

I need to insert into a sql script a BLOB field from a binary file, loaded from a relative path from the project that owns that script. 我需要将二进制文件中的BLOB字段插入到SQL脚本中,该二进制文件是从拥有该脚本的项目的相对路径中加载的。

I've searched and found this aproach: insert a BLOB via a sql script? 我搜索并找到了这个方法: 通过sql脚本插入BLOB? But RAWTOHEX function seems to work with Strings, and my input is a binary file. 但是RAWTOHEX函数似乎适用于Strings,而我的输入是一个二进制文件。

Any ideas? 有任何想法吗?

From FILE_READ documentation: FILE_READ文档中:

File names and URLs are supported. 支持文件名和URL。 To read a stream from the classpath, use the prefix classpath: 要从类路径读取流,请使用前缀类路径:

Seems that the use of a relative path it's not possible; 似乎无法使用相对路径; then a possible solution is to include the file with the desired binary content in the classpath and access it using classpath: in FILE_READ . 那么可能的解决方案是在classpath包含具有所需二进制内容的文件,并使用FILE_READ classpath:对其进行访问。 This way you can deploy it in any other machine without worries about the absolute paths. 这样,您可以将其部署在任何其他计算机上,而不必担心绝对路径。

By code using RunScript 通过使用RunScript代码

So if before perform your test you setup the DB running the script by code using something like: 因此,如果在执行测试之前,通过使用类似以下代码的代码来设置运行脚本的数据库:

RunScript.execute(conn, new FileReader("yourScript.sql"));

Then add the logo.png as a resource of your project this way you can refer it inside the script using classpath: notation: FILE_READ('classpath:/your/package/resource/logo.png') . 然后以这种方式将logo.png添加为项目的资源,您可以在脚本中使用classpath:来引用它classpath:表示法: FILE_READ('classpath:/your/package/resource/logo.png')

Using RunScript from command line tool 从命令行工具使用RunScript

If you use the command line tool, you can create a .jar to package your resources, eg resource.jar and add it to classpath in your cmd: 如果使用命令行工具,则可以创建一个.jar来打包资源,例如resource.jar并将其添加到cmd中的classpath中:

java -cp h2*.jar;resource.jar org.h2.tools.RunScript -url jdbc:h2:~/test -script yourScript.sql

Then as the previous case in your script you can refer your binary file using FILE_READ('classpath:/your/package/resource/logo.png') 然后,与脚本中的前一种情况一样,您可以使用FILE_READ('classpath:/your/package/resource/logo.png')引用二进制文件。

Hope it helps, 希望能帮助到你,

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

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