0

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

2
  • 1
    It is in the App's secure sandbox, use a root-able emulator image (i.e. "System Image", not the "Google API Image" or "Google Play Image") or just copy/save the db file to a non-secure location like : stackoverflow.com/questions/54126671/… Commented Dec 16, 2020 at 10:33
  • @Viol1997 Anything update? Commented Dec 21, 2020 at 1:39

1 Answer 1

1

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.

According to your sqlite database path, it is internal storage, you can not find the database file on an emulator unless it's rooted. Then you can find the database file in data/data/com.companyname/files.

Opening cmd, and enter Adb Root to root android emulator, then enter Adb shell to verify if it is successful.

enter image description here

If you want to export sqlite database to csv file, please take a look following thread:

Exporting SQLite Database to csv file in android

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.