简体   繁体   English

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

[英]I cannot successfully insert and delete values in sqlite wp8.1

When I launch my application first time the code runs successfully and inserts the value. 当我第一次启动应用程序时,代码成功运行并插入值。 But when I click on remove button only first time the selected item will be removed. 但是,当我仅在第一次单击“删除”按钮时,所选项目将被删除。 When I add again after deleting one value it is showing an exception 当我删除一个值后再次添加时,它显示异常

catastrophic failure (exception from hresult: 0x8000ffff (e_unexpected)) 灾难性故障(异常结果:0x8000ffff(e_unexpected))

Even I cannot delete a value 即使我无法删除值

  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;

How to add and delete values also update the values from sqlite wp8.1 如何添加和删除值还可以从sqlite wp8.1更新值

(Here list is table and list1 is column) (这里列表是表,列表1是列)

This catastrophic failure is many because of adding list items in two ways in the above code. 由于在上述代码中以两种方式添加列表项,因此造成了灾难性的失败。 So just insert the values to the textbox and assign it to list. 因此,只需将值插入文本框并将其分配给列表即可。 To insert a list of items to the database follow the below step 要将项目列表插入数据库,请执行以下步骤

 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";
        }

We can also delete the same values with the simple code 我们也可以使用简单的代码删除相同的值

 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();}

So selected item can be deleted from the list (Here list is the class name and list1 is the column name) 因此,可以从列表中删除所选项目(此处的列表是类名,而list1是列名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM