C# 无法从Xamarin应用程序中的SQLite DB中删除
我有一个类产品来存储有关产品的一些信息,并希望将其保存到Xamarin应用程序中的SQLite DB中C# 无法从Xamarin应用程序中的SQLite DB中删除,c#,linq,sqlite,xamarin,xamarin.android,C#,Linq,Sqlite,Xamarin,Xamarin.android,我有一个类产品来存储有关产品的一些信息,并希望将其保存到Xamarin应用程序中的SQLite DB中 class Product { [PrimaryKey, AutoIncrement, Column("ID")] public int ID { get; set; } public string Name { get; set; } public string Category { get; set; } public Product(strin
class Product
{
[PrimaryKey, AutoIncrement, Column("ID")]
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public Product(string name, string category)
{
Name = name;
Category = category;
}
public Product()
{
}
}
在我的代码中,我创建了一个产品对象作为
Product s = new Product();
s.Name = some_name;
s.Category = some_category;
并通过以下方式在我的数据库中插入:
var db = new SQLiteConnection(db_path);
var table = db.Table<Product>();
db.Insert(s);
我无法从表中删除关联的记录。您是否尝试在Product.cs中保存产品类:`
[Serializable()]
public class Product
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }`
}
在创建表中:
“创建表产品(ID整数主键,…是否尝试将产品类保存在Product.cs中:`
[Serializable()]
public class Product
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }`
}
在创建表中:
“创建表产品(ID INTEGER主键,…表中的产品ID不能为0,因为此字段标记为PrimaryKey。创建一个方法来获取所有项目,以便您可以验证它们的ID。方法
deleteThisProduct()
您只能传入productIdprivate void deleteThisProduct(int-productId)
因为这就是您正在使用的所有内容。这就是为什么我觉得这一切都很奇怪。ID设置不正确,这会在调用db.Delete()时产生问题。产品ID在表中不能为0,因为此字段标记为PrimaryKey。创建一个方法来获取所有项目,以便您可以验证它们的ID。方法deleteThisProduct()
您只能传入productId私有void deleteThisProduct(int productId)
,因为这是您正在使用的全部内容。这就是为什么我发现所有这些都很奇怪的原因。ID设置不正确,这会在调用db时产生问题。Delete()产品已在Product.cs中。我唯一没有做的事情是添加[Serializable()]在开始时,您是否已将db字段声明为表定义中的主键?是的。当我将产品类传递给db.CreateTable()时,会自动执行此操作您是否在sqlite编辑器中打开了db文件并查看了产品表的架构。应该有ID integer not null prlmary key autoincrement。我不知道如何执行此操作,因为创建db时,它会存储在隐藏位置。我猜移动应用程序总是这样。产品已在product.cs中。唯一的我没有做的事情是在开始时添加[Serializable()]您是否在表定义中将db字段声明为主键?是的。这是在我将产品类传递给db.CreateTable()时自动完成的您是否在sqlite编辑器中打开了db文件并查看了产品表的架构。应该有ID integer not null prlmary key autoincrement。我不知道如何做,因为创建db时,它会存储在隐藏位置。我想移动应用程序总是这样。
[Serializable()]
public class Product
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }`
}