【问题标题】:java.sql.SQLException: SQL logic error or missing database, SQLite, JDBCjava.sql.SQLException: SQL 逻辑错误或缺少数据库、SQLite、JDBC
【发布时间】:2011-01-28 04:11:42
【问题描述】:

我使用 Java 中的 JDBC 创建了与 SQLite 的数据库连接。我的 SQL 语句执行正确,但有时我在使用 conn.commit() 时收到以下错误:

java.sql.SQLException: SQL logic error or missing database

任何人都可以帮助我如何避免此类问题。有没有更好的调用 JDBC 程序的方法?

Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:/home/Data/database.db3");
conn.setAutoCommit(false);

String query = "Update Chits set BlockedForChit = 0 where ServerChitID = '" + serverChitId + "' AND ChitGatewayID = '" + chitGatewayId + "'";
        Statement stmt = conn.createStatement();
        try {
            stmt.execute(query);
            conn.commit();
            stmt.close();
            stmt = null;
        }

【问题讨论】:

  • 如果stmt为null,你怎么调用.execute呢?
  • 你是怎么解决这个问题的?在调用 commit().... 时遇到同样的错误 :(
  • 确保将 conn 添加到每个使用它的类/方法中,这为我解决了问题。

标签: java sqlite jdbc


【解决方案1】:

您的变量 serverChitId 和 chitGatewayId 是否包含会破坏 SQL 的字符?使用 PreparedStatements 通常更安全:

PreparedStatement ps = conn.prepareStatement("Update Chits set BlockedForChit = 0 where ServerChitID = ? AND ChitGatewayID = ?");
ps.setString(1, serverChitId);
ps.setString(2, chitGatewayId);
ps.executeUpdate();

这样,JDBC 驱动程序负责确保对字符串进行必要的转义。

【讨论】:

    【解决方案2】:

    尝试将conn.setAutoCommit 设置为true。您还需要删除conn.commit();。 如果您在函数内部执行此操作,请使您的函数同步。 如果使用 PreparedStatement 而不是 Statement,那就更好了。 所有这一切都在发生,因为有时您尝试同时连接和修改数据库,并且由于最后一个连接尚未提交,它会引发该异常。当您将其设置为 autoCommit 时,它会自行处理流程。(这对我来说真的很痛苦,因为它什么也没说,我阅读了所有 org.sqlite.DB 文件以找出这一点)

    Class.forName("org.sqlite.JDBC");
    conn = DriverManager.getConnection("jdbc:sqlite:/home/Data/database.db3");
    conn.setAutoCommit(true);
    
    PreparedStatement ps = "Update Chits set BlockedForChit = 0 where ServerChitID = ? AND ChitGatewayID = ? ";
            ps.setString(1, serverChitId); 
            ps.setString(2, chitGatewayId);
    
            try {
                ps.executeUpdate();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多