Recently, I have been developing an android application where I shipped an sqlite database with the application. The Sqlite is initially stored at assets folder, and then copied to the proper directory (/data/data/[package]/databases/[db-name]) upon launch. I had the following piece of code for checking the existence of a database.
public boolean databaseExist()
{
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
checkDB.close();
return true;
}
catch(SQLiteException e) {
// Log an info message stating database doesn't exist.
}
return false;
}
Even though I was catching an exception, however, I was still getting the error message (sqlite3_open_v2 failed):
sqlite3_open_v2("...', &handle, 6, NULL) open failed
Doing some googling shows that its a popular problem.
Finally, I tried an old school java code with java.io.File, and then it seemed to work fine. So I modified the code like below:
public boolean databaseExist()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
And, it works very well, I also noticed (not confirmed yet), that this appears to be somewhat faster than the previous openDatabase approach
I hope this helps someone out.
P.S: if you are looking for a good tutorial on handling sqlite database on Android, please check out the following site: Using your own Sqlite database on Android
Comments
Awesome stuff. I was getting
Awesome stuff. I was getting the error and came across your blog. Thanks.
Great post. I think the
Great post.
I think the function should be public (as private cannot be called from outside)
Regards,
Jason
Hi Jason, Thanks for pointing
Hi Jason,
Thanks for pointing that out. It has been fixed now (private changed to public).
I actually invoked these methods internally within the class, hence I made them private, and when I posted them on blog, I forgot to change the visibility to public.
I've been working to resolve
I've been working to resolve this issue for some days and came across this blog...
I got the same error "sqlite3_open_v2("...', &handle, 6, NULL) open failed" while trying to copy the database..
I changed the code as mentioned by you, but still it didn't work.. It throws "FileNotFoundException"
W/System.err( 1180): java.io.FileNotFoundException: MDB.sqlite
W/System.err( 1180): at android.content.res.AssetManager.openAsset(Native Method)
W/System.err( 1180): at android.content.res.AssetManager.open(AssetManager.java:313)
You can take a look at the entire source code in the following URL..
https://docs.google.com/leaf?id=0B6T3Dz5rsfrrNTY0OTQxZTEtNGM0Mi00ODJhLWJ...
Any help would be greatly appreciated.
Peter
Unfortunately, I am not able
Unfortunately, I am not able to see your code. I assume its DB_Project > src > org > me. Its all empty there.
Hi alif, I am facing same
Hi alif,
I am facing same problem even after i changed the code as you mentioned for check db. I m using the same code there in article http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-androi....
Strange is same code works on android 2.1 emulator and device but not on >=2.2 emulator and device.
Please help...
I am getting exception like:
08-31 13:10:56.942: INFO/Database(328): sqlite returned: error code = 14, msg = cannot open file at source line 25467
08-31 13:10:56.942: ERROR/Database(328): sqlite3_open_v2("/data/data/com.test.ui/databases/pcm.sqlite", &handle, 1, NULL) failed
08-31 13:10:56.984: WARN/System.err(328): java.lang.StringIndexOutOfBoundsException
08-31 13:10:57.042: WARN/System.err(328): at android.app.ContextImpl.validateFilePath(ContextImpl.java:1579)
08-31 13:10:57.051: WARN/System.err(328): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:539)
08-31 13:10:57.061: WARN/System.err(328): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
08-31 13:10:57.091: WARN/System.err(328): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
08-31 13:10:57.142: WARN/System.err(328): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
08-31 13:10:57.162: WARN/System.err(328): at com.test.db.controller.SQLiteDBAdapter.createDataBase(SQLiteDBAdapter.java:63)
08-31 13:10:57.191: WARN/System.err(328): at com.test.db.controller.DBController.createDataBase(DBController.java:69)
08-31 13:10:57.201: WARN/System.err(328): at com.test.ui.UiVariables.(UiVariables.java:57)
08-31 13:10:57.242: WARN/System.err(328): at com.test.ui.UiVariables.initUiVariable(UiVariables.java:33)
08-31 13:10:57.252: WARN/System.err(328): at com.test.ui.PigmyCollector.onCreate(PigmyCollector.java:53)
08-31 13:10:57.272: WARN/System.err(328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-31 13:10:57.315: WARN/System.err(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-31 13:10:57.331: WARN/System.err(328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-31 13:10:57.351: WARN/System.err(328): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-31 13:10:57.384: WARN/System.err(328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-31 13:10:57.402: WARN/System.err(328): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 13:10:57.411: WARN/System.err(328): at android.os.Looper.loop(Looper.java:123)
08-31 13:10:57.411: WARN/System.err(328): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 13:10:57.421: WARN/System.err(328): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 13:10:57.442: WARN/System.err(328): at java.lang.reflect.Method.invoke(Method.java:521)
08-31 13:10:57.451: WARN/System.err(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 13:10:57.472: WARN/System.err(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 13:10:57.472: WARN/System.err(328): at dalvik.system.NativeStart.main(Native Method)
08-31 13:10:57.522: DEBUG/AndroidRuntime(328): Shutting down VM
08-31 13:10:57.522: WARN/dalvikvm(328): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): FATAL EXCEPTION: main
08-31 13:10:57.641: ERROR/AndroidRuntime(328): java.lang.Error: Error copying database
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.test.db.controller.SQLiteDBAdapter.createDataBase(SQLiteDBAdapter.java:70)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.test.db.controller.DBController.createDataBase(DBController.java:69)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.test.ui.UiVariables.(UiVariables.java:57)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.test.ui.UiVariables.initUiVariable(UiVariables.java:33)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.test.ui.PigmyCollector.onCreate(PigmyCollector.java:53)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.os.Looper.loop(Looper.java:123)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:521)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 13:10:57.641: ERROR/AndroidRuntime(328): at dalvik.system.NativeStart.main(Native Method).
From my experience, I think
From my experience, I think you get sql error code 14 if a database connection was opened and was not closed properly, but I am not sure if that applies to your case.
Sorry, I can't be of much help. Perhaps you can try asking on stackoverflow?
great information. thank you.
great information. thank you.
Thank you very much for
Thank you very much for sharing. Definitely solved my problem.
Thanks! :)
Thanks! :)
Thanks for sharing it solves
Thanks for sharing it solves my problem..
way 2222
way 2222 better.....terrific..
Came from stackoverflow to
Came from stackoverflow to your site.
http://stackoverflow.com/questions/9060754/sqlite3-open-v2-data-data-com...
It was helpful.
I obtained your blog from the
I obtained your blog from the bunch of it on identical topic. I am delighted that I hit a bingo
Thanx man it helped
Thanx man it helped
There's another way to
There's another way to resolve this problem. You can use the following as well:
File database = getApplicationContext().getDatabasePath("databasename.db"); return database.exists();This solve the problem as well.
Add new comment