Reputation: 1820
I want to create a database for an app in the data directory. I tried to use
try{
String path = "/data/data/"+context+"/database/Einkaufsliste.sqlite";
database = SQLiteDatabase.openOrCreateDatabase(path,null);
}
But I get always the following exception: unable to open database file
Logcat gives the following result:
04-10 19:55:09.387: E/SqliteDatabaseCpp(554): sqlite3_open_v2("/data/data/at.einkaufsliste/database/Einkaufsliste.sqlite", &handle, 6, NULL) failed 04-10 19:55:09.527: E/SQLiteDatabase(554): Failed to open the database. closing it. 04-10 19:55:09.527: E/SQLiteDatabase(554): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1043) 04-10 19:55:09.527: E/SQLiteDatabase(554): at at.einkaufsliste.Database.createOrOpen(Database.java:33) 04-10 19:55:09.527: E/SQLiteDatabase(554): at at.einkaufsliste.Database.(Database.java:23) 04-10 19:55:09.527: E/SQLiteDatabase(554): at at.einkaufsliste.MainActivity.onCreate(MainActivity.java:50) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.Activity.performCreate(Activity.java:4465) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.ActivityThread.access$600(ActivityThread.java:123) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.os.Handler.dispatchMessage(Handler.java:99) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.os.Looper.loop(Looper.java:137) 04-10 19:55:09.527: E/SQLiteDatabase(554): at android.app.ActivityThread.main(ActivityThread.java:4424) 04-10 19:55:09.527: E/SQLiteDatabase(554): at java.lang.reflect.Method.invokeNative(Native Method) 04-10 19:55:09.527: E/SQLiteDatabase(554): at java.lang.reflect.Method.invoke(Method.java:511) 04-10 19:55:09.527: E/SQLiteDatabase(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-10 19:55:09.527: E/SQLiteDatabase(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-10 19:55:09.527: E/SQLiteDatabase(554): at dalvik.system.NativeStart.main(Native Method)
I set the permission EXTERNAL Storage and the group permisson.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission-group android:name="android.permission-group.STORAGE"></permission-group>
What is wrong with this usage?
Upvotes: 0
Views: 204
Reputation: 1006614
What is wrong with this usage?
NEVER HARDCODE PATHS. /data/data
is wrong for many users. Use getDatabasePath()
to get the location where a database file should go on internal storage. This will also fix your problems with using string concatenation to build a path.
That would give you:
try {
database = SQLiteDatabase.openOrCreateDatabase(getDatabasePath("Einkaufsliste.sqlite"), null);
}
assuming that this code is called from a method in a subclass of Context
. Otherwise, you will need to arrange to call getDatabasePath()
on a Context
.
I set the permission EXTERNAL Storage
If by this you mean that you have WRITE_EXTERNAL_STORAGE
, that does not matter here, as you are not putting the database on external storage
and the group permisson
I have no idea what that means, sorry.
Also, please consider using SQLiteOpenHelper
, to be able to handle database schema changes over time.
Upvotes: 0