diff options
author | David A. Madore <david@procyon.(none)> | 2010-04-25 17:30:26 +0200 |
---|---|---|
committer | David A. Madore <david@procyon.(none)> | 2010-04-25 17:30:26 +0200 |
commit | cd58d91f8befc8536e94f4e5b4f55f50bea05c33 (patch) | |
tree | 3336103e6b901dec5e6de264f46567e76dbb0895 /src/org/madore/android/unicodeMap/UnicodeDatabase.java | |
parent | 0e64b9e42047a96e26395002b8287a70f2da2eea (diff) | |
download | UnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.tar.gz UnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.tar.bz2 UnicodeMap-cd58d91f8befc8536e94f4e5b4f55f50bea05c33.zip |
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).
Diffstat (limited to 'src/org/madore/android/unicodeMap/UnicodeDatabase.java')
-rw-r--r-- | src/org/madore/android/unicodeMap/UnicodeDatabase.java | 39 |
1 files changed, 29 insertions, 10 deletions
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; + } + } |