简体   繁体   English

通过 Mybatis 运行时无法识别 SQLite 命令

[英]SQLite commands not recognized when running through Mybatis

I'm trying to use Mybatis to run a SQL script to initialize a database as shownhere but get an SQLException saying "not implemented by SQLite JDBC driver".我正在尝试使用 Mybatis 运行 SQL 脚本来初始化数据库,如下所示但得到一个SQLException说“不是由 SQLite JDBC 驱动程序实现”。 I've checked that my SQL statement is valid and it works on the very same database via both the console interface and Java's PreparedStatement .我已经检查过我的 SQL 语句是否有效,并且它通过控制台界面和 Java 的PreparedStatement在同一个数据库上工作。

Here's my initialization class along with the code that successfully creates the table commented out:这是我的初始化类以及成功创建表的代码注释掉:

public void init() throws Exception {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream stream = loader.getResourceAsStream("db_init.sql");

        try (Connection conn = getConnection()) {
            ScriptRunner sr = new ScriptRunner(conn);
            sr.runScript(new InputStreamReader(stream));
            //PreparedStatement stmt = conn.prepareStatement("CREATE TABLE IF NOT EXISTS TestTable (testColumn TEXT);");
            //stmt.execute();
        } catch (Exception e) {
            throw e;
        }
    }

The SQL contained in db_init.sql is exactly the same as in the commented conn.prepareStatement() call. db_init.sql包含的 SQL 与注释的conn.prepareStatement()调用中的 SQL 完全相同。

And here is the console output this generates:这是生成的控制台输出:

CREATE TABLE IF NOT EXISTS TestTable (
  testColumn TEXT
)

Error executing: CREATE TABLE IF NOT EXISTS TestTable (
  testColumn TEXT
)
.  Cause: java.sql.SQLException: not implemented by SQLite JDBC driver
org.apache.ibatis.jdbc.RuntimeSqlException: Error executing: CREATE TABLE IF NOT EXISTS TestTable (
  testColumn TEXT
)
.  Cause: java.sql.SQLException: not implemented by SQLite JDBC driver
        at org.apache.ibatis.jdbc.ScriptRunner.executeLineByLine(ScriptRunner.java:150)
        at org.apache.ibatis.jdbc.ScriptRunner.runScript(ScriptRunner.java:110)
        at fi.basse.shamery.db.Database.init(Database.java:60)
        at fi.basse.shamery.Main.main(Main.java:19)
Caused by: java.sql.SQLException: not implemented by SQLite JDBC driver
        at org.sqlite.jdbc3.JDBC3Statement.unused(JDBC3Statement.java:387)
        at org.sqlite.jdbc3.JDBC3Statement.setEscapeProcessing(JDBC3Statement.java:382)
        at org.apache.ibatis.jdbc.ScriptRunner.executeStatement(ScriptRunner.java:230)
        at org.apache.ibatis.jdbc.ScriptRunner.handleLine(ScriptRunner.java:210)
        at org.apache.ibatis.jdbc.ScriptRunner.executeLineByLine(ScriptRunner.java:143)
        ... 3 more

Is there a way to get ScriptRunner.runScript() working or do I have to parse and execute the statements in my code?有没有办法让ScriptRunner.runScript()工作,还是我必须解析和执行代码中的语句?

Thank you!谢谢!

It's the escape processing that is not implemented by the driver.这是驱动程序没有实现的转义处理。
Disabling escape processing should work.禁用转义处理应该有效。

ScriptRunner sr = new ScriptRunner(conn);
sr.setEscapeProcessing(false);
sr.runScript(new InputStreamReader(stream));

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

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