Android - How to Use SQLiteDatabase.open?
i'm trying to use
SQLiteDatabase.openDatabase(
"/data/data/edwin11.myapp/databases/myapp.db", null, (SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS));
to create/open a database instead of making use of the SQLiteOpenHelper (because i want to pass in the flag SQLiteDatabase.NO_LOCALIZED_COLLATORS
.
However, i am getting this exception for that line of code:
04-18 09:50:03.585: ERROR/Database(3471): sqlite3_open_v2("/data/data/edwin11.myapp/databases/myapp.db", &handle, 6, NULL) failed
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): java.lang.RuntimeException: An error occured while executing doInBackground()
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
04-18 09:50:03.665: ERROR/AndroidRunt开发者_如何学Goime(3471): at java.lang.Thread.run(Thread.java:1060)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): Caused by: android.database.sqlite.SQLiteException: unable to open database file
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1584)
04-18 09:50:03.665: ERROR/AndroidRuntime(3471): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)
...
Doing some testing just before that line of code (using File.isExists
) shows that the file /data/data/edwin11.myapp/databases/myapp.db
does not exist.
Would that be the cause of the error? (Or am i just using SQLiteDatabase.openDatabase
the wrong way?)
Would it help if i create the file beforehand? (Shouldn't that be taken care of by the SQLiteDatabase.CREATE_IF_NECESSARY
flag that i passed in?)
If creating the file manually is the way to go, is it just an empty file, or do i have to write something to it?
Thanks and Regards.
Database files exist in the following path structure:
String sDbPath = "/data/data/" + YourMainActivityClass.class.getPackage().getName() + "/databases/YourDBFileName";
E.g.: If your application exists in a package named; com.foo.bar
, and the main Activity class name is BabarActivity
, with DB file name MyData.db
you can issue the above command as;
String sDbPath = "/data/data/" + com.foo.bar.BabarActivity.class.getPackage().getName() + "/databases/MyData.db";
Now, you can check whether the database file exists using this method:
boolean isDbExist() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(sDbPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
Log.d(DB_NAME, "Database does't exist yet.", e);
}
if (checkDB != null) {
checkDB.close();
checkDB = null;
return true;
} else
return false;
}
Hope this helps!
精彩评论