问题
I'm making simply windows 10 apllication - sales manager. Only 3 tables. I have local sqlite database. I'm looking for easy way to backup this database or synchronize. Because databae is small I think about full synchro always, so for example: I'm clicking 'Send database' and full data from device is send to server. When I click 'download' all database will be downloaded. I will be grateful for advice simply way to save database
回答1:
IMHO, you can take advantage of OneDrive's App Folder to backup your SQLite database.
The App Folder is a dedicated, special folder for your app. The App Folder is typically named after your app, and is found in the Apps folder in the user's OneDrive. If you request the
onedrive.appfolder
permission scope and the user authorizes it, your app gets read and write access to this folder.
And for C#, you can use OneDrive SDK for CSharp in your application. For example, you can send your database file like following:
private async Task<Item> UploadDBFile()
{
StorageFile DBFile = await ApplicationData.Current.LocalFolder.GetFileAsync("db.sqlite");
var oneDriveClient = await OneDriveClientExtensions.GetAuthenticatedUniversalClient(new[] { "onedrive.appfolder" });
using (var contentStream = await DBFile.OpenStreamForReadAsync())
{
return await oneDriveClient.Drive.Special.AppRoot.ItemWithPath("backup.sqlite").Content.Request().PutAsync<Item>(contentStream);
}
}
And download the db like:
private async Task DownloadDBFile()
{
var oneDriveClient = await OneDriveClientExtensions.GetAuthenticatedUniversalClient(new[] { "onedrive.appfolder" });
using (var contentStream = await oneDriveClient.Drive.Special.AppRoot.ItemWithPath("backup.sqlite").Content.Request().GetAsync())
{
StorageFile downloadedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("DownloadedDB.sqlite",
CreationCollisionOption.ReplaceExisting);
using (var writerStream = await downloadedFile.OpenStreamForWriteAsync())
{
contentStream.CopyTo(writerStream);
}
};
}
Please note that to use OneDrive API in your application, you need to first register your application for OneDrive by following these steps.
To sync local database, you can also try with Offline Data Sync in Azure Mobile Apps. This is more powerful than backup your database.
When your app is in offline mode, users can still create and modify data, which will be saved to a local store. When the app is back online, it can synchronize local changes with your Azure Mobile App backend. The feature also includes support for detecting conflicts when the same record is changed on both the client and the backend. Conflicts can then be handled either on the server or the client.
Offline sync has a number of benefits:
- Improve app responsiveness by caching server data locally on the device
- Create robust apps that remain useful when there are network issues
- Allow end-users to create and modify data even when there is no network access, supporting scenarios with little or no connectivity
- Sync data across multiple devices and detect conflicts when the same record is modified by two devices
- Limit network use on high-latency or metered networks
For more info, please refer to Offline Data Sync in Azure Mobile Apps, Enable offline sync for your Windows app, and the Todo offline sample in GitHub.
来源:https://stackoverflow.com/questions/38186179/sqlite-database-synchro-bakup-windows-10-mobile