【发布时间】:2017-08-18 13:18:58
【问题描述】:
我有一个使用 Xamarin for Android 编写的应用程序,它允许我存储我的客户信息。我使用 sqlite 在本地存储所有数据,一切正常。但我需要与在手机上使用相同应用程序的同事分享此信息。因此,例如,如果我从手机中添加新的客户订单,这将被保存到我的本地数据库中。 现在我想点击一个按钮来触发我本地数据库的同步,并在 Dropbox 或 Google Drive 的共享文件夹中备份此数据库存储。
根据以下建议,我正在尝试使用 DropBox Core Api。我已经到了进行身份验证以访问我的帐户的地步,现在我需要将我的本地数据库复制到 Dropbox 的应用程序文件夹中。我将下面的代码用于我的第二个活动。当它启动时,我会看到 Dropbox 的身份验证页面。之后,当我单击备份按钮时,我希望读取本地数据库并将其保存到 ./Apps/ClientsApp/ 下的 Dropbox。但我只收到一个通用错误(发生未处理的异常)。我在哪里做错了?
using Dropbox.CoreApi;
using Dropbox.CoreApi.Android;
using Dropbox.CoreApi.Android.Session;
namespace my_app
{
[Activity(Label = "Second Activity")]
class clientsDB : Activity
{
string AppKey = "myAppKey";
string AppSecret = "myAppSecret";
DropboxApi dropboxApi;
private Button backup;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.clientsDB);
// dropbox
AppKeyPair appKeys = new AppKeyPair(AppKey, AppSecret);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
dropboxApi = new DropboxApi(session);
(dropboxApi.Session as AndroidAuthSession).StartOAuth2Authentication(this);
backup = FindViewById<Button>(Resource.Id.backup);
backup.Click += Backup_Click;
}
private void Backup_Click(object sender, EventArgs e)
{
string origin = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "dbClients.db3");
string dropboxPath = @"./Apps/ClientsApp";
upload(origin, dropboxPath);
}
// use async cause I had Android.OS.NetworkOnMainThreadException
async void upload(string origin, string destination)
{
using (var input = File.OpenRead(origin))
{
// Gets the local file and upload it to Dropbox
dropboxApi.PutFile(destination, input, input.Length, null, null);
}
}
protected async override void OnResume()
{
base.OnResume();
// After you allowed to link the app with Dropbox,
// you need to finish the Authentication process
var session = dropboxApi.Session as AndroidAuthSession;
if (!session.AuthenticationSuccessful())
return;
try
{
// Call this method to finish the authentication process
session.FinishAuthentication();
}
catch (IllegalStateException ex)
{
Toast.MakeText(this, ex.LocalizedMessage, ToastLength.Short).Show();
}
}
}
}
【问题讨论】:
-
你的意思是你找不到数据库文件?您的代码中应该有一个路径,用于创建 SQLiteConnection(如果您正在使用它)。
-
我无法访问该路径。我在这里有我的数据库存储:dbPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");
-
为什么你没有访问权限?我刚刚尝试创建一个 File 变量,为其提供 DB 文件的路径,它可以工作。尝试: File a = new File(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3"));拥有 File 变量,您可以将 db 文件复制/导出到外部云存储。
-
您是尝试从应用程序中以编程方式备份文件,还是从 PC 手动备份?
-
我需要以编程方式执行此操作。例如,单击按钮将通过在我的 Dropbox/GDrive 中创建该数据库的备份副本来备份我在 mylocal db 中的所有数据
标签: c# database sqlite xamarin xamarin.android