Java 和 SQLite 问题

标签 java sql sqlite


我写了一个 java 程序,它应该用算法生成的一些行填充 sqlite 数据库。 所以我访问数据库并插入我的行... 它持续了几秒钟但是:

1.30168691E9s 100 --> '0 1 2 5 8', 0, 0, 0
3.163s 200 --> '0 1 2 7 17', 0, 0, 0
3.158s 300 --> '0 1 2 9 30', 0, 0, 0
Exception in thread "main" java.sql.SQLException: unable to open database file
at org.sqlite.DB.execute(DB.java:275)
at org.sqlite.DB.executeUpdate(DB.java:281)
at org.sqlite.Stmt.executeUpdate(Stmt.java:103)
at grid0.GridFill.fillTable(GridFill.java:26)
at grid0.GridFill.writeCombs(GridFill.java:54)
at grid0.Main.main(Main.java:15)
Java Result: 1

程序执行前 300 行,但随后崩溃...我不明白为什么。 我需要一些帮助...

谢谢...

public void fillTable( String hand) throws Exception
{
String data = "'" + hand + "', 0, 0, 0";
if(count%100 == 0)
{
System.out.println((System.currentTimeMillis()-time)/(float)1000 + "s " + count +" --> " + data);
time = System.currentTimeMillis();
}
stat.executeUpdate("insert into Hands (Hand, Lock, Done, SubstitutionNumber) VALUES (" + data + ")");
}

最佳答案

首先,我使用来自 xerial.org 的 sqlite jdbc 驱动程序运气好得多,它可以在这里找到:http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC Xerial 似乎比 zentus 更频繁地更新他们的驱动器。

使用 jdbc 时,最好使用 PreparedStatements。准备好的语句将允许您执行诸如执行一批插入之类的事情。无需执行多个 executeUpdate 调用,您可以使用批处理一次完成所有调用。有关 Batches 和 PreparedStatements 的示例,请参阅以下链接:http://download.oracle.com/javase/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame6.html http://download.oracle.com/javase/1.4.2/docs/guide/jdbc/getstart/preparedstatement.html

您需要的基本代码示例是:

PreparedStatement stmt = con.prepareStatement(
    "INSERT INTO employees('salary', 'name') VALUES (?, ?)");

stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();

stmt.setInt(1, 3000);
stmt.setString(2, "Bill Barnes");
stmt.addBatch();

// submit the batch for execution
stmt.executeBatch();

setInt(index, value) 允许您替换 '?'在具有值的 PreparedStatement 中。使用批处理和准备好的语句,您可以多次执行此操作,并在最后调用 executeBatch() 时一次执行它们。

此外,如评论者所述,请确保在完成插入后关闭与数据库的连接。此外,您还需要关闭可能通过从数据库中选择而打开的任何结果集。

希望对您有所帮助。

-亚历克斯

关于Java 和 SQLite 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5518196/

相关文章:

java - Resttemplate默认httpclient,读取超时有绝对值

java - Spring RestTemplate Jackson 反序列化数组数组

java - ModuleLayer 和 ClassLoader 有什么关系?

mysql - 设置要反转的 MySQL 表的默认模式顺序

无论定义什么数据类型,PHP sqlite 都会返回所有字符串

java - 按方法和用户进行 ReSTLet 授权

MySQL:在结果中显示未找到的字符串

c# - 无法找到列

android - 如何在 GreenDao 中使用 GROUP BY

内存中的 Python SQLite 缓存