Search code examples
sqlitexamarinxamarin.formssqlite-netsqlite-net-pcl

No result with sqlite-net-pcl, xamarin at ios only


I am trying to use sqlite on Xamarin and has been implemented sqlite-net-pcl to create and access local database. Works very well on Android but I can't read records on iOS.

                Experiences experience = new Experiences()
                {
                    Experience = experienceEntry.Text
                };

                //************************************

                string folderPath = Path.Combine(
                         System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
                        , ".." /* This is not used on Android */
                        , "Library" /* This is not used on Android */
                    );
                string fullPath = Path.Combine(folderPath, App.DB_FILE_NAME);

                //************************************

                SQLiteConnection conn = new SQLiteConnection(fullPath);
                conn.CreateTable<Experiences>();
                int rows = conn.Insert(experience);

                var x = conn.Table<Experiences>();

                conn.Close();

                if (rows > 0)
                    DisplayAlert("Success", "Experience succesfully inserted", "Ok");
                else
                    DisplayAlert("Failed", "Experience failed to be inserted", "Ok");

Experiences Class

    public class Experiences
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [MaxLength(250)]
        public string Experience { get; set; }
    }

After add a record, the Display Alert "Success" is shows up but the value of the x is null.

How I can read the records? I did try different folders and conn.Query instead of conn.Table but I am getting the same result.

Is something related with permissions? if true, why can't get an error?


Solution

  • this works for me in iOS

    protected async override void OnAppearing()
        {
            base.OnAppearing();
    
            var mainDir = FileSystem.AppDataDirectory;
            var fullPath = Path.Combine(mainDir, "data.db3");
    
            SQLiteConnection conn = new SQLiteConnection(fullPath);
            conn.CreateTable<Experiences>();
    
            for (int i = 0; i <=5; i++)
            {
                var exp = new Experiences { Experience = i.ToString() };
                conn.Insert(exp);
            }
    
            var data = conn.Table<Experiences>().ToList();
    
            await DisplayAlert("Data", data.Count.ToString(), "OK");
        }