sqlite-net-pcl没有结果,xamarin仅在ios上

inb24sb2  于 2023-08-01  发布在  SQLite
关注(0)|答案(1)|浏览(149)

我试图使用Xamarin上的sqlite,并已实现sqlite-net-pcl创建和访问本地数据库。在Android上工作得很好,但我不能在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");

字符串
体验课程

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

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


添加记录后,显示警报“成功”,但x值为空。
我怎么能读记录?我确实尝试了不同的文件夹和conn.Query而不是conn.Table,但我得到了相同的结果。
是否与权限有关?如果为真,为什么不能得到错误?

5lwkijsr

5lwkijsr1#

这在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");
    }

字符串

相关问题