0

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?

carlosbou
  • 3
  • 2
  • Add to question the declaration of `class Experiences`. Show its **properties** that should be stored in DB. – ToolmakerSteve Jul 24 '23 at 23:14
  • Is your DB file being created? AFAIK the trick to create the Library path on iOS has been outdated for years – Jason Jul 25 '23 at 00:39
  • @ToolmakerSteve I just modified the question to include the class Experiences declaration. Thanks – carlosbou Jul 25 '23 at 12:34
  • @Jason Sorry for my ignorance, what do you mean? File being created? – carlosbou Jul 25 '23 at 12:35
  • 1
    There is a file created for the db. That is what the file path is used for – Jason Jul 25 '23 at 12:40
  • 1
    Use [file system helpers](https://learn.microsoft.com/xamarin/essentials/file-system-helpers?tabs=ios) to get the path, don't use `System.Environment`. – Julian Jul 25 '23 at 13:01
  • @Jason Yes, the file has been created and the records being saved but I can't get them. This is happening only in iOS. – carlosbou Jul 25 '23 at 13:24
  • Please explain exactly what you mean by "I can't get them". When you query the table after you `Insert` is just the new record missing? Is the table empty? Have you used an SQLite admin tool to check the contents of the db? Is it possibly a timing issue - if you wait a few seconds to query the db does the new record show up? – Jason Jul 25 '23 at 13:30
  • @Jason the command `conn.Table();` is returning null. The table currently have 9 records. – carlosbou Jul 25 '23 at 14:10
  • how are you determining that it has 9 records? – Jason Jul 25 '23 at 14:19
  • @Jason I did copied the sqlite database to my desktop and open it with a sqlite client I have – carlosbou Jul 25 '23 at 15:07

1 Answers1

0

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");
    }
Jason
  • 86,222
  • 15
  • 131
  • 146