我可以有H2在内存数据库中自动创建模式吗? [英] Can I have H2 autocreate a schema in an in-memory database?

查看:894
本文介绍了我可以有H2在内存数据库中自动创建模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我已经看过 H2数据库在内存中 - 初始化模式通过Spring / Hibernate 问题;它不适用这里。)

(I've already seen the H2 database In memory - Init schema via Spring/Hibernate question; it is not applicable here.)

我想知道如果在H2中有一个设置将允许我在连接到它时自动创建模式。如果它有帮助,我只对内存中的情况感兴趣。

I'd like to know if there's a setting in H2 that will allow me to auto-create a schema upon connecting to it. If it helps, I'm only interested in the in-memory case.

H2在URL的末尾支持各种分号分隔的修饰符,但我没有找到一个用于自动创建模式。是否有这样的功能?

H2 supports various semicolon-separated modifiers at the end of the URL, but I didn't find one for automatically creating a schema. Is there such a feature?

推荐答案

是的,H2支持连接时执行SQL语句。您可以运行脚本,也可以只运行一个或两个语句:

Yes, H2 supports executing SQL statements when connecting. You could run a script, or just a statement or two:

String url = "jdbc:h2:mem:test;" + 
             "INIT=CREATE SCHEMA IF NOT EXISTS TEST"
String url = "jdbc:h2:mem:test;" + 
             "INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;" + 
                  "SET SCHEMA TEST";
String url = "jdbc:h2:mem;" + 
             "INIT=RUNSCRIPT FROM '~/create.sql'\\;" + 
                  "RUNSCRIPT FROM '~/populate.sql'";

请注意双反斜杠( \\ )仅在Java中是必需的。需要在 INIT 中的; 之前的反斜杠。

Please note the double backslash (\\) is only required within Java. The backslash(es) before ; within the INIT is required.

这篇关于我可以有H2在内存数据库中自动创建模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆