我无法成功地在sqlite wp8.1中插入和删除值

本文关键字:插入 删除 wp8 sqlite 成功 | 更新日期: 2023-09-27 18:13:20

当我第一次启动应用程序时,代码成功运行并插入值。但是当我第一次点击删除按钮时,选中的项目将被删除。当我删除一个值后再次添加时,它显示一个异常

灾难性失败(异常from hresult: 0x8000ffff (e_unexpected))

即使我不能删除值

  protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (data.Values["check"] != null)
        {
            this.Frame.Navigate(typeof(BlankPage1));
        }
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        await con.CreateTableAsync<list>();
        List<list> mylist = await con.QueryAsync<list>("select * from list");
        if (mylist.Count != 0)
        {
            list_view.ItemsSource = mylist;
            list_view.DisplayMemberPath = "list1";
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (!mypop.IsOpen)
        {
            mypop.IsOpen = true;
        }
    }
    public async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        try
        {
                list l = new list();
                l.list1 = text_input.Text.ToString();
                list_view.Items.Add(l.list1);
                await con.InsertAsync(l);
                mypop.IsOpen = false;
        }
        catch(Exception ex)
        {
           var MessageDialog = new MessageDialog(ex.Message).ShowAsync();
        }

    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        if (mypop.IsOpen)
        {
            mypop.IsOpen = false;
        }
    }
    private async void Button_Click_3(object sender, RoutedEventArgs e)
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";

        var con = new SQLiteAsyncConnection(dbpath);
        var stt = await con.QueryAsync<list>("delete from list where list1='" + list_view.SelectedItem + "'");

       update();
    }
    public async void update()
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        List<list> mylist = await con.QueryAsync<list>("select * from list");
        if (mylist.Count != 0)
        {
            list_view.ItemsSource = mylist;
            list_view.DisplayMemberPath = "list1";
        }
    }
    Windows.Storage.ApplicationDataContainer data = Windows.Storage.ApplicationData.Current.LocalSettings;

如何添加和删除值并从sqlite wp8.1更新值

我无法成功地在sqlite wp8.1中插入和删除值

(此处list为表,list1为列)

这种灾难性的失败是因为在上面的代码中以两种方式添加列表项。所以只要将值插入文本框并将其赋值给list。按照以下步骤

向数据库插入项目列表
 public async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        try
        {
            list l = new list();
            l.list1 = text_input.Text;
            await con.InsertAsync(l);
            update();
 public async void update()
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        list_view.ItemsSource = new List<list>();
        List<list> mylist = await con.QueryAsync<list>("select * from list");
        if (mylist.Count != 0)
        {
            list_view.ItemsSource = mylist;
            list_view.DisplayMemberPath = "list1";
        }

也可以用简单的代码

删除相同的值
 private async void Button_Click_3(object sender, RoutedEventArgs e)
    {
        var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
        var con = new SQLiteAsyncConnection(dbpath);
        if (list_view.SelectedItem != null)
        {
            list k = (list)list_view.SelectedItem;
           await con.QueryAsync<list>("delete from list where list1='" + k.list1 + "'");
            update();}

因此所选项目可以从列表中删除(这里list是类名,list1是列名)