From cd58d91f8befc8536e94f4e5b4f55f50bea05c33 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 25 Apr 2010 17:30:26 +0200 Subject: Fix various brokennesses. Populate database only when necessary. I had forgotten to call db.setTransactionSuccessful() (but had failed to notice that because the database was already populated by my previous test). --- .../madore/android/unicodeMap/UnicodeDatabase.java | 39 ++++++++++++++++------ 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/org/madore/android/unicodeMap/UnicodeDatabase.java') diff --git a/src/org/madore/android/unicodeMap/UnicodeDatabase.java b/src/org/madore/android/unicodeMap/UnicodeDatabase.java index c40cf67..b81c99a 100644 --- a/src/org/madore/android/unicodeMap/UnicodeDatabase.java +++ b/src/org/madore/android/unicodeMap/UnicodeDatabase.java @@ -25,7 +25,9 @@ public class UnicodeDatabase { protected static final int DATABASE_VERSION = 1; protected static final String UNICODE_TABLE_NAME = "unicode"; - protected static class DatabaseInit extends SQLiteOpenHelper { + protected boolean needPopulate; + + protected class DatabaseInit extends SQLiteOpenHelper { final Context context; @@ -40,6 +42,7 @@ public class UnicodeDatabase { +"id INTEGER PRIMARY KEY , " +"name TEXT , " +"category TEXT )"); + UnicodeDatabase.this.needPopulate = true; } @Override @@ -59,10 +62,22 @@ public class UnicodeDatabase { this.activity = activity; this.dbinit = new DatabaseInit(activity); db = dbinit.getReadableDatabase(); + if ( ! this.needPopulate ) { + Cursor c = db.rawQuery("SELECT count(*) FROM "+UNICODE_TABLE_NAME, null); + c.moveToFirst(); + int lineCnt = c.getInt(0); + c.close(); + if ( lineCnt == 0 ) + this.needPopulate = true; + } } - protected void reportProgress(Handler progressHandler, - int done, int total) { + public boolean needPopulate() { + return this.needPopulate; + } + + protected static void reportProgress(Handler progressHandler, + int done, int total) { Message msg = progressHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("done", done); @@ -113,6 +128,8 @@ public class UnicodeDatabase { } rd.close(); } + db.setTransactionSuccessful(); + this.needPopulate = false; } catch (UnsupportedEncodingException e) { throw new AssertionError("US-ASCII encoding unsupported"); } catch (IOException e) { @@ -144,15 +161,12 @@ public class UnicodeDatabase { return ch; } - protected final static String[] countColumns = { "count(*)" }; - public int countRange(int from, int to) { final Cursor c - = db.query(UNICODE_TABLE_NAME, countColumns, - "id >= ? AND id < ?", - new String[] { Integer.toString(from), - Integer.toString(to) }, - null, null, null, null); + = db.rawQuery("SELECT count(*) FROM "+UNICODE_TABLE_NAME + +" WHERE id >= ? AND id < ?", + new String[] { Integer.toString(from), + Integer.toString(to) }); if ( c.getCount() != 1 ) throw new AssertionError("\"SELECT count(*)\" returned no result"); c.moveToFirst(); @@ -207,4 +221,9 @@ public class UnicodeDatabase { return new CursorIterable(c); } + public void close() { + db.close(); + db = null; + } + } -- cgit v1.2.3