Feature request: NO_LOCALIZED_COLLATORS on Android
I have an app that downloads an sqlite database from the web, places it in the databases directory then loads it via Titanium's database API. This works fine on iPhone, but fails on Android because a special table android_meta is missing.
It looks like the DB load would not fail if the special constant NO_LOCALIZED_COLLATORS were passed in to the open call. From the docs:
public static final int NO_LOCALIZED_COLLATORS
Since: API Level 1
Flag for openDatabase(String, SQLiteDatabase.CursorFactory, int) to open the database without support for localized collators.
This causes the collator LOCALIZED not to be created. You must be consistent when using this flag to use the setting the database was created with. If this is set, setLocale(Locale) will do nothing.
Constant Value: 16 (0x00000010)
Please consider adding a flag to the Database.open() call so we can open externally fetched databases in the future.
Edit: here is a patch which makes it the default, for anyone interested.
diff --git a/android/modules/database/src/ti/modules/titanium/database/DatabaseModule.java b/android/modules/database/src/ti/modules/titanium/database/DatabaseModule.java
index 918eb50..8b10e75 100644
--- a/android/modules/database/src/ti/modules/titanium/database/DatabaseModule.java
+++ b/android/modules/database/src/ti/modules/titanium/database/DatabaseModule.java
@@ -36,9 +36,15 @@ public class DatabaseModule extends TiModule
public TiDatabaseProxy open(String name) {
TiDatabaseProxy dbp = null;
+ Context ctx = getTiContext().getTiApp();
+ File dbPath = ctx.getDatabasePath(name);
try {
- SQLiteDatabase db = getTiContext().getTiApp().openOrCreateDatabase(name, Context.MODE_PRIVATE, null);
+ SQLiteDatabase db =
+ SQLiteDatabase.openDatabase(dbPath.getPath(),
+ null,
+ SQLiteDatabase.NO_LOCALIZED_COLLATORS |
+ SQLiteDatabase.CREATE_IF_NECESSARY);
if (DBG) {
Log.d(LCAT, "Opened database: " + name);
}
2 Answers
-
Actually, this doesn't seem to be necessary. I was trying to open a corrupted file, and the trace message complained about the android_meta table being missing so the database would be closed. But when I replaced it with a non-corrupt file, I found that the existing code works fine - so there is no need for this patch.
(the reason I thought it was working on iPhone is because ".sql" is appended to DBs on that platform, and I had another file shadowing the one I wanted)
-
Glad it worked.