简体   繁体   中英

How to export import SQLite database from/to Dropbox in Android?

I have successfully integrated dropbox with my app and can upload and can download files, but when I upload a sqlite database file then after downloading it, the file doesn't contains any data.

Here is my code how I upload:

public void upload() {

        FileInputStream inputStream = null;
        try {
            File file = new File(Environment.getExternalStorageDirectory()
                    .toString() + "/tripmileagedatabase");
            inputStream = new FileInputStream(file);
            Entry newEntry = mDBApi.putFile("/tripmileagedatabase", inputStream,
                    file.length(), null, null);
            Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
        } catch (DropboxUnlinkedException e) {
            // User has unlinked, ask them to link again here.
            Log.e("DbExampleLog", "User has unlinked.");
        } catch (DropboxException e) {
            Log.e("DbExampleLog", "Something went wrong while uploading.");
        } catch (FileNotFoundException e) {
            Log.e("DbExampleLog", "File not found.");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

and my download method is:

public void download() {

        FileOutputStream outputStream = null;
        try {
            File file = new File(Environment.getExternalStorageDirectory()
                    .toString() + "/tripmileagedatabase");
            outputStream = new FileOutputStream(file);
            DropboxFileInfo info = mDBApi.getFile("/tripmileagedatabase", null,
                    outputStream, null);
            Log.i("DbExampleLog", "The file's rev is: "
                    + info.getMetadata().rev);
            // /path/to/new/file.txt now has stuff in it.
        } catch (DropboxException e) {
            Log.e("DbExampleLog", "Something went wrong while downloading.");
        } catch (FileNotFoundException e) {
            Log.e("DbExampleLog", "File not found.");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                }
            }
        }
    }

But when I take this database in my app then it shows no data in that database, what can I do now?

Just access the sqlite file from their website on a browser and check in a sqlitebrowser to make sure data does exits in the database.

This is what i've used. Might need to modify it a bit to work with ur api. But it should work

private void saveStreamToFile(InputStream responseStream){
    try{
        byte[] buf = new byte[1024];int len;
        FileOutputStream out = getActivity().openFileOutput(
                "SQLIITE_FILE_NAME", Context.MODE_PRIVATE);
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
        responseStream.close();

        SQLiteDatabase appDb = SQLiteDatabase.openDatabase(this.getFilesDir()
                .getAbsolutePath()+"/SQLITE_FILE_NAME.db", null
                ,SQLiteDatabase.NO_LOCALIZED_COLLATORS
                |SQLiteDatabase.OPEN_READONLY);
    }catch(IOException e){
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM