开发者

Java and SQLite problem

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.30168691E9开发者_Python百科s 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 + ")");

}


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:

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) 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜