且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-18 15:44:38

来自 FILE_READ 文档:

支持文件名和URL.要从 classpath,请使用前缀classpath:

File names and URLs are supported. To read a stream from the classpath, use the prefix classpath:

似乎无法使用相对路径;那么可能的解决方案是将具有所需二进制内容的文件包含在classpath中,并使用FILE_READ中的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. This way you can deploy it in any other machine without worries about the absolute paths.

通过使用RunScript

By code using 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"));

然后将logo.png添加为项目的资源,这样您就可以使用classpath:表示法FILE_READ('classpath:/your/package/resource/logo.png')在脚本中引用它.

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').

从命令行工具使用RunScript

Using RunScript from command line tool

如果使用命令行工具,则可以创建 .jar 来打包资源,例如resource.jar并将其添加到cmd中的classpath中:

If you use the command line tool, you can create a .jar to package your resources, e.g resource.jar and add it to classpath in your cmd:

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

然后,作为脚本中的前一种情况,您可以使用FILE_READ('classpath:/your/package/resource/logo.png')

Then as the previous case in your script you can refer your binary file using FILE_READ('classpath:/your/package/resource/logo.png')

希望有帮助,