gpt4 book ai didi

java - 如何在 Android 中从/向 Dropbox 导出导入 SQLite 数据库?

转载 作者:行者123 更新时间:2023-11-30 11:42:51 24 4
gpt4 key购买 nike

我已成功将 Dropbox 与我的应用程序集成,可以上传和下载文件,但是当我上传一个 sqlite 数据库文件然后下载后,该文件不包含任何数据。

这是我上传的代码:

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) {
}
}
}
}

我的下载方式是:

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) {
}
}
}
}

但是当我在我的应用程序中使用这个数据库时,它显示该数据库中没有数据,我现在该怎么办?

最佳答案

只需在浏览器上从他们的网站访问 sqlite 文件并检查 sqlitebrowser 以确保数据确实存在于数据库中。

这是我用过的。可能需要稍微修改一下才能使用你的 api。但它应该工作

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();
}
}

关于java - 如何在 Android 中从/向 Dropbox 导出导入 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com