I created a SQLite database to store data from a hypothetical application in which a user stores recipes:
using System;
using System.IO;
using DataAccesSQLite_scratch;
using SQLite;
using Xamarin.Forms;
[assembly: Dependency(typeof(SQLiteDb))]
namespace DataAccesSQLite_scratch
{
public class SQLiteDb : ISQLiteDb
{
public SQLiteAsyncConnection GetConnection()
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var path = Path.Combine(documentsPath, "MySQLite.db3");
return new SQLiteAsyncConnection(path);
}
}
}
and
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using SQLite;
using Xamarin.Forms;
namespace DataAccesSQLite_scratch
{
public class Recipe : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
private string _name;
[MaxLength(225)]
public string Name
{
get { return _name; }
set
{
// code below assures that if the value isn't changed, this event should not be called
if (_name == value)
return;
_name = value;
OnPropertyChanged();
}
}
}
public partial class MainPage : ContentPage
{
private SQLiteAsyncConnection _connection;
private ObservableCollection<Recipe> _recipes;
public MainPage()
{
InitializeComponent();
_connection = DependencyService.Get<SQLiteDb>().GetConnection();
}
// By convention use the "OnAppearing()" method and don't put underlying code in the constructor
protected override async void OnAppearing()
{
await _connection.CreateTableAsync<Recipe>();
var recipes = await _connection.Table<Recipe>().ToListAsync();
_recipes = new ObservableCollection<Recipe>(recipes);
recipesListView.ItemsSource = _recipes;
base.OnAppearing();
}
async void OnAdd(object sender, System.EventArgs e)
{
var recipe = new Recipe { Name = "Recipe" + DateTime.Now.Ticks };
await _connection.InsertAsync(recipe);
_recipes.Add(recipe);
}
async void OnUpdate(object sender, System.EventArgs e)
{
var recipe = _recipes[0];
recipe.Name += " UPDATED";
await _connection.UpdateAsync(recipe);
}
async void OnDelete(object sender, System.EventArgs e)
{
var recipe = _recipes[0];
await _connection.DeleteAsync(recipe);
_recipes.Remove(recipe);
}
}
}
All the methods work and are properly stored. So when I close the application and start it again, the data is persisted successfully. However, how can I access this data in my emulator? And how could I then export this data to e.g. a .csv or .json format? As can be seen, it should be in the SpecialFolder.MyDocuments. but I couldn't find it on the emulator itself.
Thanks in advance