无法从sqlite数据库中删除选中的一行

时间:2014-11-04 11:05:25

标签: c# sqlite windows-phone-8

我想通过其Id删除该行,但我不能通过其Id.like删除它,例如值 date | time | Floor | zone | Latitude |经度,我想在选择它时删除它的一行,但我不能。我是写下所有主要功能的类

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

// public string isMarkPoint {get;组; }

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

}

当我点击删除,即方法delete_click我不能删除行

1 个答案:

答案 0 :(得分: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);
}