微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLITE 删除项目不会真正删除

如何解决SQLITE 删除项目不会真正删除

我有一个 xamarin 表单应用程序,问题是当我从 sqlite 表中删除一个项目时,看起来一切正常,该项目从集合中删除,网格更新等,但是当我重新启动应用程序,该项目仍然存在。就像删除只在内存中有效,但它从未保存在数据库中。

我的代码在下面

我在我的 App 构造函数中创建了一个名为 DB 的实例

    public partial class App
    {
        static Database database;

        public static Database DB
        {
            get
            {
                if (database == null)
                {
                    string nombreBD = "MyDataBaseFile.db3";
                    string _databasePath = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory,nombreBD);
                    database = new Database(_databasePath);
                }
                return database;
            }
        }
................
}

我将 sqlite 与从类创建的表一起使用,就像这样

db = new sqliteAsyncConnection(dbPath);
db.CreateTableAsync<MyType>().Wait();

其中 MyType 是这样的类

public class MyType
{   
        [PrimaryKey]
        public int Idtable { get; set; }

        ......
}

我尝试像这样删除表格中的一行:

var x = await App.DB.GetItemAsync<MyType>(obj.Idtable );
int regsDeleted = await App.DB.DeleteItemAsync<MyType>(x);

GetItemsAsync 基本上是:await db.FindAsync<T>(id);

public async Task<T> GetItemAsync<T>(int id) where T : new()
    {
        try
        {
            return await db.FindAsync<T>(id);
        }
        catch (System.Exception ex)
        {
            throw new System.Exception($"Error sqlLite {MethodBase.GetCurrentMethod().Name}: {ex.Message}");
        }
    }

删除方法是这样的:

public async Task<int> DeleteItemAsync<T>(T item) where T : new()
{
    try
    {
        int regsDeleted=await db.DeleteAsync(item);
        db.GetConnection().Commit();
        return regsDeleted;
    }
    catch (System.Exception ex)
    {
        throw new System.Exception($"Error sqlLite {MethodBase.GetCurrentMethod().Name}: {ex.Message}");
    }
}

就像我说的那样,我没有出现任何错误,看起来一切正常,但是当重新启动应用程序时,该项目仍然存在!!
任何的想法?可以在连接中添加一些东西吗?交易?...任何帮助都会很棒

谢谢

UPDATE 经过大量测试我意识到问题不在于删除。问题是,每次我通过 USB 电缆将应用程序从 VS 运行到我的 android 设备时,我不知道数据库如何或为什么从某些备份中恢复,我不知道何时或在哪里完成。看起来 Android 有备份和我的应用程序的“数据”,当新版本出现时,他只是恢复数据。我读到说 Xamarin.Essentials.FileSystem.AppDataDirectory 不应该用于保存数据库,所以问题是。保存 sqlLite 数据库的正确位置在哪里。有什么想法吗?我的应用程序没有部署空数据库,我的应用程序在第一次执行时创建了数据库。有谁知道如何避免文件夹的恢复?每次我从 VisualStudio 运行应用程序时?

解决方法

DeleteAsync 在没有 Commit 的情况下工作。我对您的代码进行了更改。它对我有用。

我添加了 PrimaryKeyAutoIncrement 属性以确保 SQLite.NET 数据库中的每个 Note 实例都有一个由 SQLite.NET 提供的唯一 ID。

 public class MyType
{
    [PrimaryKey,AutoIncrement]
    public int Idtable { get; set; }
    public string Text { get; set; }

}

连接数据库、保存记录、删除行和获取所有项目的代码。

readonly string _databasePath = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory,"MyDataBaseFile.db3");

    SQLiteAsyncConnection database;
    public MyType myType { get; set; }
    int i = 0;

    public Page2()
    {
        InitializeComponent();
    }

    private void Connect_Clicked(object sender,EventArgs e)
    {
        database = new SQLiteAsyncConnection(_databasePath);
        database.CreateTableAsync<MyType>().Wait();
    }

    async void Save_Clicked(object sender,EventArgs e)
    {
        myType = new MyType() { Text = "Hello" + i };
        if (myType.Idtable != 0)
        {
            // Update an existing note.
            await database.UpdateAsync(myType);
            i++;
        }
        else
        {
            // Save a new note.
            await database.InsertAsync(myType);
            i++;
        }

    }

    async void Delete_Clicked(object sender,EventArgs e)
    {
        var x = await database.FindAsync<MyType>(myType.Idtable);
        int regsDeleted = await database.DeleteAsync(x);

    }

    async void Get_Clicked(object sender,EventArgs e)
    {
        var s = await database.Table<MyType>().ToListAsync();
         try
        {
            var s2 = await database.FindAsync<MyType>(myType.Idtable);
        }
        catch
        {
            return;
        }
    }
}

请注意,如果我重新启动应用程序,则没有 myType.Idtable。所以我使用 try catch 来运行我的项目。

为数据库添加四项并删除最后一项。

enter image description here

重启应用后,项目:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。