gpt4 book ai didi

java - H2数据库-从sql文件创建表

转载 作者:行者123 更新时间:2023-12-02 02:42:54 25 4
gpt4 key购买 nike

我正在尝试从 SQL 文件创建内存中的 H2 数据库,如 docs 中所示。 .

我将 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;

我不确定为什么它需要插入语句。

有人可以帮忙吗

最佳答案

您需要从连接 URL 中的 SQL 命令中间删除意外的第二个 INIT=

                  + ";INIT=create schema if not exists " + schema + "\\;SET SCHEMA " + schema + "\\;"

+ /* problem was here */ "runscript from '" + pathToSchemaSql + "'"

关于java - H2数据库-从sql文件创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com