我正在尝试从列表视图中删除所选项目。值存储在数据库中。我在sqlite中尝试过。如何删除数据库中存在的值并更新列表视图
private void Button_Click_3(object sender, RoutedEventArgs e)
{
var dbpath = ApplicationData.Current.LocalFolder.Path + "/Mydb1.db";
var con = new SQLiteAsyncConnection(dbpath);
var stt=con.QueryAsync<list>("delete from list where list1='"+list_view.SelectedItem+"'");
con.DeleteAsync(stt);
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";
}
}
这里list1是列名,列表是表名
我放置了一个断点并执行了代码。它显示了一个异常(无法删除任务`1:它没有PK)我认为PK是异常中的列名,一旦从sqlite类检查下面的代码,那里触发了异常
public int Delete (object objectToDelete)
{
var map = GetMapping (objectToDelete.GetType ());
var pk = map.PK;
if (pk == null) {
throw new NotSupportedException ("Cannot delete " + map.TableName + ": it has no PK");
}
var q = string.Format ("delete from \"{0}\" where \"{1}\" = ?", map.TableName, pk.Name);
return Execute (q, pk.GetValue (objectToDelete));
}
答案 0 :(得分:0)
更改您的SQL查询。 在查询中分配(list_view.Selecteditem)时,数据库无法识别所选值,因为列表中的多个项目对selectitem语句很熟悉。因此,首先存储所选项目并将其传递给查询。
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();
}
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";
}
}