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)); 

create / open a database instead of using SQLiteOpenHelper (because I want to pass the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag.

However, I get this exception for this 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/AndroidRuntime(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 this line of code (using File.isExists ) shows that the file /data/data/edwin11.myapp/databases/myapp.db does not exist.

Will this be the cause of the error? (Or am I just using the wrong path in SQLiteDatabase.openDatabase ?)

Would I help create the file in advance? (Shouldn't I take care of the SQLiteDatabase.CREATE_IF_NECESSARY flag I passed in?)

If you manually create the file, is it just an empty file or do I need to write something?

Thanks and respect.

+4
source share
1 answer

Database files exist in the following path structure:

  String sDbPath = "/data/data/" + YourMainActivityClass.class.getPackage().getName() + "/databases/YourDBFileName"; 

For example: if your application exists in a package named; com.foo.bar , and the main name of the BabarActivity activity BabarActivity , with the file name DB MyData.db you can execute the above command as:

 String sDbPath = "/data/data/" + com.foo.bar.BabarActivity.class.getPackage().getName() + "/databases/MyData.db"; 

Now you can check if 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!

0
source

Source: https://habr.com/ru/post/1307243/


All Articles