我正在尝试将预加载的数据库从资产文件夹复制到可以读取的位置,但是当我的代码尝试调用InputStream来获取数据库时,我得到了IOException 从资产文件夹中。
我一直在关注Reign Design上的教程以及SO的出色答案:https://stackoverflow.com/a/9109728/3699923。我的问题与Error copying SQLite DB from Asset folder非常相似,但是他遇到了文件未找到错误,并且 mine是一个更通用的“错误复制文件” 。
我还尝试过从设备中删除该应用,然后再次进行测试,以确保它与设备上已存在的新文件无关。
我的DBHelper文件(这是节选):
public class AppDataDBHelper extends SQLiteOpenHelper {
private static String TAG = "AppDataDBHelper"; // Tag just for the LogCat window
private static String DB_NAME ="appdata.db";// Database name
private static String DB_PATH = "";
private static String DB_TABLE = "Bonus_Data";
private static int DB_VERSION = 1;
private SQLiteDatabase mDataBase;
private final Context mContext;
public AppDataDBHelper(Context context) {
super(context, DB_NAME, null,DB_VERSION);
this.mContext = context;
DB_PATH = context.getAssets() + "/databases/";
// DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
Log.e(TAG,DB_PATH + DB_NAME);
}
public void createDataBase() throws IOException
{
//If the database does not exist, copy it from the assets.
Log.e(TAG,"Entered createDatabase");
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assets
Log.e(TAG,"createDatabase passing to copyDatabase");
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
Log.e(TAG,"entered checkDatabase");
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
Log.e(TAG,"entered copyDatabase");
close();
Log.e(TAG,"copyDatabase close");
InputStream mInput = mContext.getAssets().open(DB_NAME);
Log.e(TAG,"copyDatabase inputStream");
String outFileName = DB_PATH + DB_NAME;
Log.e(TAG,DB_PATH + " | " + DB_NAME);
OutputStream mOutput = new FileOutputStream(outFileName);
Log.e(TAG,"copyDatabase outputStream");
FileHelper.copyFile(mInput, mOutput);
getWritableDatabase().close();
}
来自我相关活动文件的呼叫:
// Handle the appData DB.
SQLiteDatabase appDB = null;
appDBHelper = new AppDataDBHelper(this);
try {
Log.e(TAG,"Calling createDataBase");
appDBHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
Log.e(TAG,"Calling openDatabase");
appDBHelper.openDataBase();
} catch(SQLException sqle){
throw sqle;
}
还有我在logcat中看到的错误:
2019-02-02 22:40:29.103 603-603/net.tommyc.android.tourofhonor E/splashScreen: Going to Bonus List
2019-02-02 22:40:29.170 603-603/net.tommyc.android.tourofhonor D/ScrollView: initGoToTop
2019-02-02 22:40:29.195 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: android.content.res.AssetManager@9353a47/databases/appdata.db
2019-02-02 22:40:29.195 603-603/net.tommyc.android.tourofhonor E/bonusListing: Calling createDataBase
2019-02-02 22:40:29.195 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: Entered createDatabase
2019-02-02 22:40:29.195 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: entered checkDatabase
2019-02-02 22:40:29.224 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: createDatabase passing to copyDatabase
2019-02-02 22:40:29.224 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: entered copyDatabase
2019-02-02 22:40:29.224 603-603/net.tommyc.android.tourofhonor E/AppDataDBHelper: copyDatabase close
2019-02-02 22:40:29.224 603-603/net.tommyc.android.tourofhonor D/AndroidRuntime: Shutting down VM
2019-02-02 22:40:29.225 603-603/net.tommyc.android.tourofhonor E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.tommyc.android.tourofhonor, PID: 603
java.lang.Error: ErrorCopyingDataBase
at net.tommyc.android.tourofhonor.AppDataDBHelper.createDataBase(AppDataDBHelper.java:57)
at net.tommyc.android.tourofhonor.bonusListing.onCreate(bonusListing.java:53)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:2)
[更新:已测试的确定代码] 我已经将代码从复制数据库更新为从数据库表中读取一组名称。
对于访问存储在资产文件夹中的外部sqlite数据库,此功能对我来说是正常的,请检查一下。
data inserted into external sqlite database but not saving in android studio
对于您的情况,我建议进行以下更改:
1)我在sqlite dbbrowser中的表格:
HttpServletResponse
2)在您的AppDataDBHelper类中:
@ControllerAdvice
public class FooBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if(response instanceof ServletServerHttpResponse) {
ServletServerHttpResponse res= (ServletServerHttpResponse)(response);
res.getServletResponse().getStatus(); //get the status code
res.getHeaders().set("fooHeader", "fooValue"); //modify headers
res.getHeaders().setETag("33a64df551425fcc55e4d42a148795d9f25f89d4") //use "type safe" methods to modify header
}
return body;
}
}
2)最后在您活动的onCreate()方法中创建数据库,如下所示:
CREATE TABLE `Bonus_Data` (
`id` INTEGER NOT NULL,
`name` TEXT NOT NULL,
PRIMARY KEY(`id`)
);