Java 和 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
线程“主”java.sql.SQLException 中出现异常:无法打开数据库文件
在 org.sqlite.DB.execute(DB.java:275)
在 org.sqlite.DB.executeUpdate(DB.java:281)
在 org.sqlite.Stmt.executeUpdate(Stmt.java:103)
在 grid0.GridFill.fillTable(GridFill.java:26)
在 grid0.GridFill.writeCombs(GridFill.java:54)
在 grid0.Main.main(Main.java:15)
Java 结果:1
程序运行前 300 行,但随后崩溃了......我不明白为什么。 我需要一些帮助...
谢谢...
public void fillTable( String hand) 抛出异常
{
字符串数据 = "'" + 手 + "', 0, 0, 0";
if(计数%100 == 0)
{
System.out.println((System.currentTimeMillis()-time)/(float)1000 + "s " + count +" --> " + data);
时间 = System.currentTimeMillis();
}
stat.executeUpdate("插入手中 (Hand, Lock, Done, SubstitutionNumber) VALUES (" + data + ")");
}
I wrote a java program that should fill a sqlite database with some rows generated by an algorithm.
So I access the db and I insert my rows...
It goes for some seconds but:
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
the program goes for the first 300 rows, but then crashes... and I can't understand why.
I need some help...
Thank you...
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 + ")");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我使用来自 xerial.org 的 sqlite jdbc 驱动程序获得了更好的运气,它可以在这里找到: http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
Xerial 似乎比 zentus 更频繁地更新驱动器。
使用jdbc 时,最好使用PreparedStatements。准备好的语句将允许您执行诸如执行一批插入之类的操作。您可以批量执行所有操作,而不是执行多个executeUpdate 调用。有关批处理和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
您需要的基本代码示例是:
setInt(index, value) 允许您替换 '?'在PreparedStatement 中带有一个值。使用批处理和准备好的语句,您可以多次执行此操作,并在最后调用executeBatch() 时一次性执行所有这些操作。
另外,正如评论者所述,请确保在完成插入后关闭与数据库的连接。此外,您还需要关闭可能通过从数据库中选择打开的任何结果集。
我希望这有帮助。
-亚历克斯
First off I've had much better luck using the sqlite jdbc driver from xerial.org it can be found here: http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
Xerial seems to update their drive much more often than zentus.
When working with jdbc it is best to use PreparedStatements. Prepared Statements will allow you do things like execute a batch of inserts. Instead of executing multiple executeUpdate calls you can do them all at once with a batch. See the following links for examples of Batches and 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
The basic code example you'll need is:
setInt(index, value) allows you to replace the '?' in the PreparedStatement with a value. With the batch and the preparedstatement you can do this multiple times and execute them all at once at the end when you call executeBatch().
Also as stated by the commenters make sure to close the connection to the DB once you're done inserting. Also you'll want to close any Result Sets that might be opened by selecting from the DB.
I hope this helps.
-Alex