Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从sqlite数据库中删除选定的行_C#_Sqlite_Windows Phone 8 - Fatal编程技术网

C# 无法从sqlite数据库中删除选定的行

C# 无法从sqlite数据库中删除选定的行,c#,sqlite,windows-phone-8,C#,Sqlite,Windows Phone 8,我想按Id删除行,但不能按Id删除行。例如,值为 date | time | Floor | zone | Latitude |经度,我想在选择它时删除它的一行,但我不能。下面是我编写所有主要函数的类 public class DbHelper { SQLiteConnection dbConn; public async Task<bool> onCreate(string DB_PATH) { try {

我想按Id删除行,但不能按Id删除行。例如,值为 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();
            //});
        }
    }

}
{ [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{get;set;}

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


当我单击“删除”即方法“删除”时,单击“我无法删除行”

编辑:我错误地剪切和粘贴了您的代码…您的对齐非常差:)

好吧,我想我终于让你的代码运行了,你的问题就在这里

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;


“但我不能通过Id删除它”是什么意思?您有所选行的ID吗?delete语句是否引发异常?@marton我更新了上面的一些代码,id是自动递增的。当我选择该行并单击delete(delete|u click method in history.xaml.cs)@chuboosaurus software时,我想删除该行(日期|时间|楼层|区域|纬度|经度)。很抱歉,我不明白你想说什么。我试图写这个。Selected\u HistoryId=listitem.Id,而不是int Selected\u HistoryId=listitem.Id;但它会出现错误,谢谢你查看我的代码,正如你所说,我试图使用它,但它仍然无法删除;然后,我使用了上面的方法,正如您所说,我对其进行了编码;最后在delete_click方法中,我编码了Db_helper.DeleteContact(选中的\u HistoryId)@vivekshahi:因为传递了错误的id,所以在
ListData\u SelectionChanged
事件中创建了一个本地
int Selected\u HistoryId
。但是您在DeleteContact函数中使用的是static属性。您从未设置导致0值的静态属性。不存在行id为0之类的行。请稍等,我会在解决方案中让它更清楚一点。@vivekshahi现在看看解决方案,您的属性是静态的(Selected_HistoryId),因此,您需要以这种方式对其进行更改,而不是创建另一个与之类似的变量。感谢您提供的示例我理解您所说的内容,并尝试按照您所说的方式实现,但当我运行代码时,我仍然无法删除该行
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);
}