Reputation: 1225
I am extending SQLiteAssetHelper
class to use my pre-populated database from assets folder but my app crashed and gave an error saying Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
.
When I checked in ddms
, there was no databases folder or my db
file in data/data/com.sqlitetospinner1/
. So to just test, I created a folder named databases
and pushed the db file into it. After this the app started to work perfectly.
It means my AssetsHelper
class failed to copy database.
AssetsHelper
class:-
public class AssetsHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "mydb.sqlite";
private static final int DATABASE_VERSION = 1;
public AssetsHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public List<String> getAllColleges(){
List<String> colleges = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM colleges_list ORDER BY Organization_Name";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
colleges.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning colleges
return colleges;
}
LogCat:-
08-25 08:41:21.927: W/SQLiteAssetHelper(27504): copying database from assets...
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): Couldn't open mydb.sqlite for writing (will try read-only):
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/mydb.sqlite file (or .zip, .gz archive) in assets, or target folder not writable
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at android.content.res.AssetManager.openAsset(Native Method)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at android.content.res.AssetManager.open(AssetManager.java:316)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at android.content.res.AssetManager.open(AssetManager.java:290)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at com.crm.AssetsHelper.getAllColleges(AssetsHelper.java:38)
08-25 08:41:22.107: E/SQLiteAssetHelper(27504): at dalvik.system.NativeStart.main(Native Method)
08-25 08:41:22.117: E/SQLiteLog(27504): (14) cannot open file at line 30191 of [00bb9c9ce4]
08-25 08:41:22.117: E/SQLiteLog(27504): (14) os_unix.c:30191: (2) open(/data/data/com.sqlitetospinner1/databases/mydb.sqlite) -
08-25 08:41:22.167: E/SQLiteDatabase(27504): Failed to open database '/data/data/com.sqlitetospinner1/databases/mydb.sqlite'.
08-25 08:41:22.167: E/SQLiteDatabase(27504): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
08-25 08:41:22.167: E/SQLiteDatabase(27504): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
08-25 08:41:22.167: E/SQLiteDatabase(27504): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
08-25 08:41:22.167: E/SQLiteDatabase(27504): at com.crm.AssetsHelper.getAllColleges(AssetsHelper.java:38)
Upvotes: 2
Views: 4695
Reputation: 2574
Upvotes: 0
Reputation: 52366
Make sure you have the db file under assets\databases folder in your app, in the main folder.
Also make sure you have specified the correct database name, so if your file name is data.db, your database name should be data.db.
public class MyHelperDatabase extends com.readystatesoftware.sqliteasset.SQLiteAssetHelper {
private static final String DATABASE_NAME = "data.db";
Or if you are using Room:
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
@VisibleForTesting
public static final String DATABASE_NAME = "data.db";
Upvotes: 0
Reputation: 404
Right click on your database file in the Package Explorer,
& click on the Properties.
inside the Resource Tab make sure the "Write" Permission is Checked for the "Owner" and "Read" Permission is Checked for all the "Users".
Upvotes: 0
Reputation: 152817
Missing databases/mydb.sqlite file (or .zip, .gz archive) in assets, or target folder not writable
Do you have the database file in your
assets/databases
folder?no, in assets folder only
SQLiteAssetHelper
expects to find the prepopulated database file in databases
folder under assets
.
Upvotes: 6