无法使用SQLiteAssetHelper类复制数据库



我正在扩展SQLiteAssetHelper类以使用我从资产文件夹中预填充的数据库,但我的应用程序崩溃并给出一个错误说Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

当我检查ddms时,在data/data/com.sqlitetospinner1/中没有数据库文件夹或我的db文件。因此,为了进行测试,我创建了一个名为databases的文件夹,并将db文件放入其中。

这意味着我的AssetsHelper类无法复制数据库。

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)

Missing databases/mydb.sqlite file (or .zip, .gz archive) in assets, or target folder not writable

您的assets/databases文件夹中有数据库文件吗?

没有,在assets文件夹中只有

SQLiteAssetHelper期望在assets下的databases文件夹中找到预填充的数据库文件。

包浏览器

中右键单击数据库文件

,点击属性

Resource选项卡内的

确保Write"检查所有者的权限和""检查所有用户的权限

请确保在应用程序的主文件夹下的assetsdatabases文件夹下有db文件。

还要确保指定了正确的数据库名称,因此,如果文件名是data.db,则数据库名称应该是data.db。

public class MyHelperDatabase extends com.readystatesoftware.sqliteasset.SQLiteAssetHelper {
    private static final String DATABASE_NAME = "data.db";

或者如果你使用的是Room:

public abstract class AppDatabase extends RoomDatabase {
    private static AppDatabase INSTANCE;
    @VisibleForTesting
    public static final String DATABASE_NAME = "data.db";
  1. 错误: "Missing databases/EDMTQuiz2019.db file".
  2. 原因:我的文件夹没有正确命名…它是资产/数据库,而不是assets/databases
  3. 解决方案:我通过重构/重命名文件夹来解决我的问题"database"改为"databases"
  4. Summary:新的正确路径是assets/databases (.db文件在database文件夹中)

相关内容

  • 没有找到相关文章

最新更新