我正试图根据docs所示的SQL文件创建内存中的H2数据库。
我将 tables.sql 文件保存在 src / main / resources / sql / 文件夹中,并尝试按以下方式创建数据库。
public static HikariConfig getHikariConfigH2(String schema, String pathToSchemaSql) { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:h2:mem:" + schema + ";DATABASE_TO_UPPER=false;" + "MODE=MySQL" + ";DB_CLOSE_DELAY=-1" + ";INIT=create schema if not exists " + schema + "\\;SET SCHEMA " + schema + "\\;" + "INIT=runscript from '" + pathToSchemaSql + "'" ); config.setUsername("sa"); config.setPassword(""); return config; } public void testSetup() { > HikariConfig configurationMaster = getHikariConfigH2("schema_name", "src/main/resources/sql/tables.sql"); DataSource master = new HikariDataSource(configurationMaster); }
但失败并出现以下异常
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INIT[*]=runscript from 'src/main/resources/sql/tables.sql'"; expected "INSERT"; SQL statement:
INIT=runscript from 'src/main/resources/sql/tables.sql' [42001-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:451) ~[h2-1.4.199.jar:1.4.199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) ~[h2-1.4.199.jar:1.4.199]
at org.h2.message.DbException.getSyntaxError(DbException.java:243) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Parser.getSyntaxError(Parser.java:991) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Parser.parsePrepared(Parser.java:951) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Parser.parse(Parser.java:788) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Parser.parse(Parser.java:764) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Parser.prepareCommand(Parser.java:683) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.Session.prepareLocal(Session.java:627) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.CommandList.executeRemaining(CommandList.java:50) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.CommandList.update(CommandList.java:63) ~[h2-1.4.199.jar:1.4.199]
at org.h2.command.Command.executeUpdate(Command.java:267) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.Engine.openSession(Engine.java:252) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:178) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.Engine.createSession(Engine.java:161) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.Engine.createSession(Engine.java:31) ~[h2-1.4.199.jar:1.4.199]
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:336) ~[h2-1.4.199.jar:1.4.199]
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:169) ~[h2-1.4.199.jar:1.4.199]
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:148) ~[h2-1.4.199.jar:1.4.199]
at org.h2.Driver.connect(Driver.java:69) ~[h2-1.4.199.jar:1.4.199]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81) [HikariCP-3.2.0.jar:na]
tables.sql文件的内容为
CREATE TABLE item_types (
id int NOT NULL,
typeName varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我不确定为什么会期望插入语句。
有人可以帮忙吗
答案 0 :(得分:0)
您需要从连接URL中的SQL命令中间删除意外的第二个INIT=
。
+ ";INIT=create schema if not exists " + schema + "\\;SET SCHEMA " + schema + "\\;"
+ /* problem was here */ "runscript from '" + pathToSchemaSql + "'"