![](/img/trans.png)
[英]I am getting errors when trying to load a Universal Windows App in the Visual Studio 2015 Simulator
[英]Trying to delete entries from SQLite db in Visual Studio 2015, c#, Universal Windows App, no errors, but not deletes
我似乎無法從ListView中刪除綁定數據庫的條目。 我可以看到數據庫中的所有內容,並添加了復選框,我選擇它並不重要...它不會刪除條目。 我瘋了,我已經嘗試了我能找到的一切。 所以這是我的listview代碼
好的,並更新。 當我投入
var existingconact = conn2.Query<Medications>("select * from Medications where Id = 4").FirstOrDefault();
我單擊按鈕,它將刪除它並更新表。 所以我對Id做錯了。
<StackPanel Margin="20, 240, 0 0">
<ListView Header="Medications" x:Name="myList" Background="DimGray" HorizontalAlignment="Right" Margin="0,0,0,0" Width="600">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="600">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox HorizontalAlignment="Right" VerticalAlignment="Center" x:Name="CheckBoxItem"/>
<TextBlock x:Name="Id" Grid.Column="1" Text="{Binding Id}" TextWrapping="Wrap" />
<TextBlock x:Name="medName" Grid.Column="2" Text="{Binding MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="3" x:Name="medDose" Text="{Binding MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="4" x:Name="whatFor" Text="{Binding WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
這是我用來嘗試刪除條目的按鈕命令
private void btn_Remove_Click(object sender, RoutedEventArgs e)
{
DBPath2 = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn2 = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath2))
{
var existingconact = conn2.Query<Medications>("select * from Medications where Id = ?").FirstOrDefault();
if (existingconact != null)
{
conn2.RunInTransaction(() =>
{
conn2.Delete(existingconact);
});
如果你想看看我的架構
public class Medications
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string MedName { get; set; }
public string MedDose { get; set; }
public string WhatFor { get; set; }
}
好的,我明白了! 這是Meds.xaml.cs中的一個小行
這是處理點擊的按鈕。
private void btn_Remove_Click(object sender, RoutedEventArgs e)
{
DBPath2 = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn2 = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath2))
{
// **This as where the error was**
var existingconact = conn2.Query<Medications>("select * from Medications where **Id = Id"**).FirstOrDefault();
if (existingconact != null)
{
conn2.RunInTransaction(() =>
{
conn2.Delete(existingconact);
});
myList.ItemsSource = conn2.Table<Medications>();
}
}
}
}
}
您從未傳入ID以查找SQLite記錄。 這一行:
var existingconact = conn2.Query<Medications>("select * from Medications where Id = ?").FirstOrDefault();
需要傳遞Id的值:
var existingconact = conn2
.Query<Medications>(
"select * from Medications where Id = ?",
myList.SelectedItem.Id)
.FirstOrDefault();
現在,從所選行獲取ID是您需要處理的。 您可能必須將myList.SelectedItem
為要編譯的預期數據類型。
您可以通過將硬編碼測試重試為參數化測試來驗證這一點:
var existingconact = conn2.Query<Medications>("select * from Medications where Id = ?", 4).FirstOrDefault();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.