2015-09-19 23 views
1

當我第一次啓動我的應用程序時,代碼成功運行並插入值。但是,當我第一次點擊刪除按鈕時,選定的項目將被刪除。當我刪除一個值後重新添加它顯示異常我無法在sqlite中成功插入和刪除值wp8.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的值

(這裏列表是列表和列表1是列)

回答

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

所以選擇的項目可以從列表 被刪除,刪除相同的值(在這裏列表是類名,列表1是列名)