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

时间:2015-09-19 06:37:13

标签: c# sqlite windows-phone-8.1

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

  

灾难性失败(来自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

中的值

(此处列表为table,list1为列)

1 个答案:

答案 0 :(得分:1)

这种灾难性的失败很多,因为在上面的代码中以两种方式添加列表项。因此,只需将值插入文本框并将其分配给列表即可。要将项目列表插入数据库,请执行以下步骤

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

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