Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Restore sqlite database in xamarin forms

$
0
0

I implemented backup file and it copy my database to sdcard. Below is apart of my code:

var destinationPath = string.Format("/sdcard/MyTaskDatabase_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", System.DateTime.Now);

after that i implemented restore backup like below:

`async void RestorDbClickedAsync(object sender, EventArgs e)
{
string[] mimeType = { "application/x-sqlite3" };
try
{
FileData filedata = await CrossFilePicker.Current.PickFile(mimeType);

        var fileAccessHelper = DependencyService.Get<IFileAccessHelper>();
        var getFilePath = string.Format("/sdcard/{0:dd-MM-yyyy_HH-mm-ss-tt}", filedata.FileName);

        fileAccessHelper.CopyAndRestore(getFilePath);
    }
    catch (Exception ex)
    {
        await DisplayAlert("Error", ex.Message.ToString(), "ok");
    }
}`

and Function CopyAndRestore() in class FileAccessHelper is like below:

`public void CopyAndRestore(string sourcePath)
{
var dbFileName = "TodoSQLite.db3";
TodoItemDatabase yourSQliteRepository = new TodoItemDatabase(GetLocalFilePath(dbFileName));
string destinationPath = GetLocalFilePath(dbFileName);

    yourSQliteRepository.DeleteAllDatabase();
    yourSQliteRepository.CloseDb();

    System.IO.File.Copy(sourcePath, destinationPath, true);
}`

My code doesn't have any error but it doesn't work and restore the database
Do you have any idea?
Can you help me please?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>