Android Sqlite数据库另存为文件

Android Sqlite数据库另存为文件,android,database,sqlite,dropbox,Android,Database,Sqlite,Dropbox,我已经构建了一个用于管理事务的应用程序,目前正在添加dropbox备份。我通过将数据库文件上传到dropbox(看起来似乎显示正确)来实现这一点。然后我想再次下载文件并覆盖现有数据库。当我这样做时,数据库将保存为文件。按context.fileList()列出;而不是context.databaseList();如何处理数据库文件以将它们放在正确的位置 以下是我认为相关的代码: private static class Downloader extends AsyncTask<Intege

我已经构建了一个用于管理事务的应用程序,目前正在添加dropbox备份。我通过将数据库文件上传到dropbox(看起来似乎显示正确)来实现这一点。然后我想再次下载文件并覆盖现有数据库。当我这样做时,数据库将保存为文件。按context.fileList()列出;而不是context.databaseList();如何处理数据库文件以将它们放在正确的位置

以下是我认为相关的代码:

private static class Downloader extends AsyncTask<Integer, Integer, Boolean>{
    Context context;
    @Override
    protected void onPreExecute(){
        context = SpendoBase.getContext();
    }
    @Override
    protected Boolean doInBackground(Integer... arg0) {
        System.out.println("DoInBackground:");
        try {
            List<DropboxAPI.Entry> entries = mDBApi.metadata("/", -1, null, true, null).contents;
            File file;
            FileOutputStream os;
            int count = 0;
            for(DropboxAPI.Entry entry: entries){
                count++;
                System.out.println("Entry.path(): " + entry.path  + " " + count + "/" + entries.size());
                file = new File(entry.path);
                System.out.println("1");
                os = context.openFileOutput(file.getName(), Context.MODE_PRIVATE);
                System.out.println("2");
                DropboxFileInfo info = mDBApi.getFile(entry.path, null, os, null);
                os.flush();
                os.close();
                System.out.println("3 " + info);
            }

        } catch (DropboxException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

private static class Uploader extends AsyncTask<Integer, Integer, Boolean>{
    String[] databaseList;
    Context context;
    @Override
    protected void onPreExecute(){
        context = SpendoBase.getContext();
        databaseList = context.databaseList();
    }
    @Override
    protected Boolean doInBackground(Integer... params) {
        for(String dbName: databaseList){
            try {
                File f = context.getDatabasePath(dbName);
                FileInputStream  fis = new FileInputStream(f.getPath());
                mDBApi.putFileOverwrite("/" + dbName, fis, f.length(), null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DropboxException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

} 
private static class MetaReader extends AsyncTask<Integer, Integer, List<String>>{

    @Override
    protected List<String> doInBackground(Integer... arg0) {
        try {

            List<String> result = new Vector<String>();
            DropboxAPI.Entry existingEntry = mDBApi.metadata("/", -1, null, true, null);
            List<DropboxAPI.Entry> temp = existingEntry.contents;
            for(int i = 0; i < temp.size(); i++){
                File f = new File(temp.get(i).path);
                result.add(f.getName());
            }
            return result;
         } catch (DropboxException e) {
             System.out.println("Something went wrong: " + e);
         }
        return null;
    }
    @Override
    protected void onPostExecute(List<String> result){
        for(String str:result){
            System.out.println(str);
        }
    }
}
私有静态类下载程序扩展异步任务{
语境;
@凌驾
受保护的void onPreExecute(){
context=SpendoBase.getContext();
}
@凌驾
受保护的布尔doInBackground(整数…arg0){
System.out.println(“DoInBackground:”);
试一试{
列表条目=mDBApi.metadata(“/”,-1,null,true,null).contents;
文件;
FileOutputOS;
整数计数=0;
for(DropboxAPI.Entry:entries){
计数++;
System.out.println(“Entry.path():“+Entry.path+”“+count+”/“+entries.size());
文件=新文件(entry.path);
系统输出打印项次(“1”);
os=context.openFileOutput(file.getName(),context.MODE\u PRIVATE);
系统输出打印项次(“2”);
DropboxFileInfo=mDBApi.getFile(entry.path,null,os,null);
os.flush();
os.close();
系统输出打印项次(“3”+信息);
}
}捕获(DropBoxexException e){
e、 printStackTrace();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
}
私有静态类上载程序扩展异步任务{
字符串[]数据库列表;
语境;
@凌驾
受保护的void onPreExecute(){
context=SpendoBase.getContext();
databaseList=context.databaseList();
}
@凌驾
受保护的布尔doInBackground(整数…参数){
for(字符串dbName:databaseList){
试一试{
文件f=context.getDatabasePath(dbName);
FileInputStream fis=新的FileInputStream(f.getPath());
mDBApi.putFileOverwrite(“/”+dbName,fis,f.length(),null);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(DropBoxexException e){
e、 printStackTrace();
}
}
返回null;
}
} 
私有静态类元读取器扩展异步任务{
@凌驾
受保护列表doInBackground(整数…arg0){
试一试{
列表结果=新向量();
DropboxAPI.Entry existingEntry=mDBApi.metadata(“/”,-1,null,true,null);
列表温度=existingEntry.contents;
对于(int i=0;i
我没有做太多的Android开发,所以我在这里可能有点离谱,但是为什么你不能在
下载程序中再次使用
context.getDatabasePath(dbName)
并将文件写入该路径?

我设法解决了这个问题。我的错误仅仅是我把数据库保存在了错误的地方

更改:

 file = new File(entry.path);
            System.out.println("1");
            os = context.openFileOutput(file.getName(), Context.MODE_PRIVATE);
致:


已解决问题

您需要根目录下目标手机,将文件保存在/data/data/com.SverkerSbrg.SpendoFull/databases/此位置。

您可以直接将任何文件存储在内部或外部存储器上。请从获取帮助以存储文件。
  file = new File("/data/data/com.SverkerSbrg.SpendoFull/databases/" + entry.path);
                System.out.println("1");
                os = new FileOutputStream(file.getPath());