Friday, 2 September 2011

SQLITE HOW TO: SQLITE: - "Database is locked" - Solution

Ok, took me a while to find but stubborn "Database is locked" problem with sqlite3 happens when sqlite connection is not closed after a transaction. So if you open it like so:


var connection = DriverManager.getConnection("jdbc:sqlite:test.db");
var statement = connection.createStatement();
var resultSet = statement.executeQuery("select * from mytable;");


Than you have to close it like this:


resultSet.close();
statement.close();
connection.close();


This should prevent you from getting "sqlite database is locked" problem.
But if you already have it happened to you, you can clear the lock by running this in your terminal:


sqlite3 test.db .dump | sqlite3 test.db.bak
rm test.db
sqlite3 test.db.bak .dump | sqlite3 test.db
rm test.db.bak

No comments:

Post a Comment