gpt4 book ai didi

c# - 我无法在 sqlite wp8.1 中成功插入和删除值

转载 作者:行者123 更新时间:2023-12-01 20:03:17 24 4
gpt4 key购买 nike

当我第一次启动应用程序时,代码成功运行并插入值。但是,当我第一次单击“删除”按钮时,所选项目才会被删除。当我删除一个值后再次添加时,它显示异常

catastrophic failure (exception from 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中的值

(这里list是表,list1是列)

最佳答案

这种灾难性的失败很多是因为在上面的代码中以两种方式添加列表项。因此,只需将值插入文本框并将其分配给列表即可。要将项目列表插入数据库,请按照以下步骤操作

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

因此可以从列表中删除所选项目(这里list是类名,list1是列名)

关于c# - 我无法在 sqlite wp8.1 中成功插入和删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com