我正在扩展SQLiteAssetHelper
类以使用我在资源文件夹中预先填充的数据库但我的应用程序崩溃并发出错误Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
。
当我签入ddms
时,db
中没有数据库文件夹或data/data/com.sqlitetospinner1/
文件。因此,为了进行测试,我创建了一个名为databases
的文件夹,并将db文件推入其中。在此之后,应用程序开始完美运行。
这意味着我的AssetsHelper
类无法复制数据库。
AssetsHelper
课程: -
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)
答案 0 :(得分:5)
Missing databases/mydb.sqlite file (or .zip, .gz archive) in assets, or target folder not writable
您的
assets/databases
文件夹中是否有数据库文件?不,仅在资产文件夹中
SQLiteAssetHelper
希望在databases
下的assets
文件夹中找到预先填充的数据库文件。
答案 1 :(得分:0)
右键单击 Package Explorer 中的数据库文件
&安培;单击“属性”。
在资源标签内,确保&#34; 写&#34;检查权限是否为&#34; 所有者&#34;和&#34; 阅读&#34;检查所有&#34; 用户&#34;。
的权限
答案 2 :(得分:0)
确保您的应用程序中的assets \ databases文件夹下的db文件位于主文件夹中。
还要确保指定了正确的数据库名称,因此如果您的文件名是data.db,则您的数据库名称应为data.db。
public class MyHelperDatabase extends com.readystatesoftware.sqliteasset.SQLiteAssetHelper {
private static final String DATABASE_NAME = "data.db";
或者如果您使用的是房间:
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
@VisibleForTesting
public static final String DATABASE_NAME = "data.db";
答案 3 :(得分:0)