我有一个位于资产/数据库/中的数据库。我使用库android-sqlite-asset-helper试图存档数据库,没有帮助。错误都消失了
E/SQLiteAssetHelper: Couldn't open databases/brodsky.db for writing (will try read-only):
com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/databases/brodsky.db file (or .zip, .gz archive) in assets, or target folder not writable
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:336)
at android.content.res.AssetManager.open(AssetManager.java:310)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
at rodionova.lyubov.brodsky.db.PoemsProvider.query(PoemsProvider.java:40)
at android.content.ContentProvider.query(ContentProvider.java:1140)
at android.content.ContentProvider$Transport.query(ContentProvider.java:262)
at android.content.ContentResolver.query(ContentResolver.java:506)
DbHelper:
public class PoemsDbHelper extends SQLiteAssetHelper {
private static String DB_NAME = "databases/brodsky.db";
private static final int DB_VERSION = 1;
public PoemsDbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
}
以及一个在日志错误中指定的ContentProvider类
公共类PoemsProvider扩展了ContentProvider { 私有静态最终UriMatcher URI_MATCHER =新的UriMatcher(UriMatcher.NO_MATCH); 私有静态最终int POEMS = 1; 私有静态最终int POEM = 2; 私有静态最终int FACTS = 3; private static final int BIOGRAPHY = 4;
static {
URI_MATCHER.addURI(PoemsContract.AUTHORITY, "poems", POEMS);
URI_MATCHER.addURI(PoemsContract.AUTHORITY, "poems/#", POEM);
URI_MATCHER.addURI(PoemsContract.AUTHORITY, "facts", FACTS);
URI_MATCHER.addURI(PoemsContract.AUTHORITY, "biography", BIOGRAPHY);
}
private PoemsDbHelper poemsDbHelper;
@Override
public boolean onCreate() {
poemsDbHelper = new PoemsDbHelper(getContext());
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
@Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase db = poemsDbHelper.getReadableDatabase();
switch (URI_MATCHER.match(uri)) {
case POEMS:
if (TextUtils.isEmpty(sortOrder)) {
sortOrder = PoemsContract.Poems.COLUMN_ID + " DESC";
}
return db.query(PoemsContract.Poems.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
case POEM:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
selection = PoemsContract.Poems._ID + " = ?";
selectionArgs = new String[]{id};
} else {
selection = selection + " AND " + PoemsContract.Poems._ID + " = ?";
String[] newSelectionArgs = new String[selectionArgs.length + 1];
System.arraycopy(selectionArgs, 0, newSelectionArgs, 0, selectionArgs.length);
newSelectionArgs[newSelectionArgs.length - 1] = id;
selectionArgs = newSelectionArgs;
}
case FACTS:
if (TextUtils.isEmpty(sortOrder)) {
sortOrder = PoemsContract.Facts.COLUMN_ID + " DESC";
}
return db.query(PoemsContract.Facts.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
case BIOGRAPHY:
if (TextUtils.isEmpty(sortOrder)) {
sortOrder = PoemsContract.Biography.COLUMN_ID + " DESC";
}
return db.query(PoemsContract.Biography.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
default:
return null;
}
}
答案 0 :(得分:0)
代替private static String DB_NAME = "databases/brodsky.db";
您应该使用private static String DB_NAME = "brodsky.db";
SQliteAssetHelper在自述文件中包括以下内容:-
像通常使用SQLiteOpenHelper一样扩展SQLiteAssetHelper, 为构造函数提供数据库名称和版本号:
public class MyDatabase extends SQLiteAssetHelper { private static final String DATABASE_NAME = "northwind.db"; private static final int DATABASE_VERSION = 1; public MyDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } }
SQLiteAssetHelper依赖资产文件和文件夹命名 约定。您的资产文件夹将在您的项目下 root,如果使用默认的gradle项目,则在src / main下 结构体。至少,您必须提供以下内容:
资产内的数据库文件夹 文件名与您提供的数据库名称匹配的数据库文件夹 在代码中(包括文件扩展名,如果有的话)对于上面的示例, 该项目将包含以下内容:
assets/databases/northwind.db