2014-11-04 105 views
1

我想通过它的Id删除行,但我不能删除它的Id.like例如值为 date | time | Floor | zone | Latitude | longitude和I want删除它,而选择它的行,但我cannot.below是我写的所有主要功能无法从sqlite数据库中删除选定的行

public class DbHelper 
{ 


    SQLiteConnection dbConn; 



    public async Task<bool> onCreate(string DB_PATH) 
    { 
     try 
     { 
      if (!CheckFileExists(DB_PATH).Result) 
      { 
       using (dbConn = new SQLiteConnection(DB_PATH)) 
       { 
        dbConn.CreateTable<historyTableSQlite>(); 
       } 
      } 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 


    private async Task<bool> CheckFileExists(string fileName) 
    { 
     try 
     { 
      var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 

    //retrieve all list from the database 
    public ObservableCollection<historyTableSQlite> ReadHistory() 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      List<historyTableSQlite> myCollection = dbConn.Table<historyTableSQlite>().ToList<historyTableSQlite>(); 
      ObservableCollection<historyTableSQlite> HistoryList = new ObservableCollection<historyTableSQlite>(myCollection); 
      return HistoryList; 
     } 
    } 

    // Insert the new info in the histrorytablesqlite table. 
    public void Insert(historyTableSQlite newcontact) 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      dbConn.RunInTransaction(() => 
      { 
       dbConn.Insert(newcontact); 
      }); 
     } 
    } 

    public void AddInfo() 
    { 
     //string f = Checkin.Floor_st; 
     Debug.WriteLine(Checkin.a); 
     string z = Checkin.Zone_st; 
     DbHelper Db_helper = new DbHelper(); 
     Db_helper.Insert((new historyTableSQlite 
     { 
      Date = DateTime.Now.ToShortDateString(), 
      Time = DateTime.Now.ToShortTimeString(), 
      Zone = "D", 
      Floor = "7", 
      latitude =12344.66, 
      longtitude = -122.56 
     })); 

    } 


    // Delete specific contact 
    public void DeleteContact(int Id) 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      var existingvalue = dbConn.Query<historyTableSQlite>("select * from historyTableSQlite where Id =" + Id).FirstOrDefault(); 
      if (existingvalue != null) 
      { 
       dbConn.RunInTransaction(() => 
       { 
        dbConn.Delete(existingvalue); 
       }); 
      } 
     } 
    } 

    //Delete all contactlist or delete Contacts table 
    public void DeleteAllContact() 
    { 
     using (var dbConn = new SQLiteConnection(App.DB_PATH)) 
     { 
      //dbConn.RunInTransaction(() => 
      // { 
      dbConn.DropTable<historyTableSQlite>(); 
      dbConn.CreateTable<historyTableSQlite>(); 
      dbConn.Dispose(); 
      dbConn.Close(); 
      //}); 
     } 
    } 

} 
下面

是我展示

public partial class History : PhoneApplicationPage 
{ 
    ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>(); 
    DbHelper Db_helper = new DbHelper(); 
    //int Selected_HistoryId; 
    public static int Selected_HistoryId { get; set; } 





    // string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); 

    public History() 
    { 

     InitializeComponent(); 

    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 

     Db_helper.AddInfo(); 
     ReadHistoryList_Loaded(); 


     // Selected_HistoryId = int.Parse(NavigationContext.QueryString["SelectedHistoryID"]); 
    } 

    public void ReadHistoryList_Loaded() 
    { 
     ReadAllContactsList dbhistory = new ReadAllContactsList(); 
     DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts 
     ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList(); 

     //Latest contact ID can Display first 

    } 

    public void ListData_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (ListData.SelectedIndex != -1) 
     { 
      historyTableSQlite listitem = ListData.SelectedItem as historyTableSQlite; 
      int Selected_HistoryId = listitem.Id; 
     } 



    } 

    public void Delete_Click(object sender, EventArgs e) 

    { 
     Db_helper.DeleteContact(Selected_HistoryId); 
    } 

    private void DeleteAll_Click(object sender, EventArgs e) 
    { 
     DbHelper Db_helper = new DbHelper(); 
     Db_helper.DeleteAllContact();//delete all db 
     DB_HistoryList.Clear(); 
     ListData.ItemsSource = DB_HistoryList; 

    } 






    //public void updateDB(string fl,string zo,double la, double lo) 
    //{ 

    // using (var db = new SQLiteConnection(dbPath)) 
    // { 
    //  var existing = db.Query<historyTableSQlite>("select * from historyTableSQlite").FirstOrDefault(); 
    //  if (existing != null) 
    //  { 
    //   existing.Floor = fl; 
    //   existing.Zone = zo; 
    //   existing.latitude = la; 
    //   existing.longtitude = lo; 
    //   db.RunInTransaction(() => 
    //   { 
    //    db.Update(existing); 
    //   }); 
    //  } 




    // } 


    //} 

    //public void AddDb(string fl, string zo, double la, double lo) 
    //{ 
    // string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); 
    // using (var db = new SQLiteConnection(dbPath)) 
    // { 
    //  db.RunInTransaction(() => 
    //  { 
    //   db.Insert(new historyTableSQlite() 
    //   { 
    //    Date = DateTime.Today.ToShortDateString(), 
    //    Time = DateTime.Now.ToShortTimeString(), 
    //    Floor = fl, 
    //    Zone = zo, 
    //    longtitude = la, 
    //    latitude = lo 
    //   }); 
    //   Debug.WriteLine(db); 
    //  }); 

    // } 





} 

我更新的值类的类表类,使其易于理解

public class historyTableSQlite : INotifyPropertyChanged 

{ [SQLite.PrimaryKey,SQLite.AutoIncrement]

public int Id { get; set; } 
private int idvalue; 

private string dateValue = string.Empty; 

public string Date { 
    get { return this.dateValue; } 
    set 
    { 
     if (value != this.dateValue) 
     { 
      this.dateValue = value; 
      NotifyPropertyChanged("Date"); 
     } 
    } 
} 


private string timeValue = string.Empty; 
public string Time 
{ 
    get { return this.timeValue; } 
    set 
    { 
     if (value != this.timeValue) 
     { 
      this.timeValue = value; 
      NotifyPropertyChanged("Time"); 
     } 
    } 
} 

private string floorValue = string.Empty; 
public string Floor 
{ 
    get { return this.floorValue; } 
    set 
    { 
     if (value != this.floorValue) 
     { 
      this.floorValue = value; 
      NotifyPropertyChanged("Floor"); 
     } 
    } 
} 

public string zoneValue; 
public string Zone 
{ 
    get { return this.zoneValue; } 
    set 
    { 
     if (value != this.zoneValue) 
     { 
      this.zoneValue = value; 
      NotifyPropertyChanged("Zone"); 
     } 
    } 
} 

private double latValue; 
public double latitude 
{ 
    get { return latValue; } 
    set 
    { 
     if (value != this.latValue) 
     { 
      this.latValue = value; 
      NotifyPropertyChanged("Latitude"); 
     } 
    } 
} 

private double lonValue; 
public double longtitude 
{ 
    get { return this.lonValue; } 
    set 
    { 
     if (value != this.lonValue) 
     { 
      this.lonValue = value; 
      NotifyPropertyChanged("Longitude"); 
     } 
    } 
} 

//公共字符串isMarkPoint {获得;组; }

public historyTableSQlite() 
{ 

} 

public historyTableSQlite(string date,string time,string floor,string zone,double lat,double lng) 
{ 
    Date = date; 
    Time = time; 
    Floor = floor; 
    Zone = zone; 
    latitude = lat; 
    longtitude = lng; 
} 
public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

}

当我点击删除IE的方法delete_click我不能删除该行

+1

是什么意思“但我不能按ID删除它”?你有选定行的ID吗?删除语句是否会引发异常? – Marton 2014-11-04 11:19:18

+0

@marton我已经更新了上面的一些代码,id是自动递增的。我想删除行(日期|时间|地板|区域|纬度|经度)当我选择它,当我点击删除(delete_click在history.xaml.cs中的方法) – 2014-11-04 12:21:03

回答

0

编辑:我剪切和粘贴代码不正确......你必须对齐很差:)

好吧,我想我终于得到了你的代码运行,你的问题是在这里

public void ListData_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (ListData.SelectedIndex != -1) 
    { 
     historyTableSQlite listitem = ListData.SelectedItem as historyTableSQlite; 

     // this will get destroy when the function exits, it's local decalartion 
     int Selected_HistoryId = listitem.Id; 

     // History.Selected_HistoryId = listitem.Id; 
     // you need to set the Property not a local variable 
    } 
} 

所做命名相同属性的局部变量应该是

History.Selected_HistoryId = listitem.Id; 

public void Delete_Click(object sender, EventArgs e) 
{ 
    Db_helper.DeleteContact(History.Selected_HistoryId); 
} 
+0

@chuboosaurus软件我很抱歉,但我不明白什么你试着说。我试图写这个.Selected_HistoryId = listitem.Id而不是int Selected_HistoryId = listitem.Id;但它给出了错误 – 2014-11-05 03:15:56

+0

谢谢你看我的代码我试图用你说的,但它仍然无法删除。我有一个局部变量以外int Selected_HistoryId;然后我上面的方法,你说我编码this.Selected_HistoryId = listitem.Id;最后在delete_click方法中我编码了Db_helper.DeleteContact(Selected_HistoryId); – 2014-11-05 03:32:44

+0

@vivekshahi它是因为你传递了错误的id,你在ListData_SelectionChanged事件中创建了一个本地'Selected_HistoryId'。但是您在DeleteContact函数中使用静态属性。您从不设置导致0值的静态属性。没有这样的行ID等于0.等一分钟,我会在解决方案中更清楚一点。 – 2014-11-05 03:41:14