如何将C# Article对象映射到SQLite articles表并解决插入异常
问题描述
我有一个SQLite表articles,以及对应的C#类定义如下:
[XmlRoot(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")] public class Article { [XmlElement(ElementName = "id", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Id { get; set; } [XmlElement(ElementName = "name", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Name { get; set; } [XmlElement(ElementName = "quantity", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Quantity { get; set; } [XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")] public string SellPrice { get; set; } [XmlElement(ElementName = "sellid", Namespace = "http://tempuri.org/DataSet1.xsd")] public string SellId { get; set; } [XmlElement(ElementName = "whole_price", Namespace = "http://tempuri.org/DataSet1.xsd")] public string WholePrice { get; set; } [XmlElement(ElementName = "group", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Group { get; set; } [XmlElement(ElementName = "barcode", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Barcode { get; set; } [XmlElement(ElementName = "measure", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Measure { get; set; } } [XmlRoot(ElementName = "DataSet1", Namespace = "http://tempuri.org/DataSet1.xsd")] public class Articles { [XmlElement(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")] public List<Article> AllArticles { get; set; } [XmlAttribute(AttributeName = "xmlns")] public string Xmlns { get; set; } }
我通过XML反序列化获取文章列表:
XmlSerializer deserializer = new XmlSerializer(typeof(Articles)); TextReader textReader = new StreamReader(xmlFilePath); Articles articles = new Articles(); articles = (Articles)deserializer.Deserialize(textReader); textReader.Close();
但当我尝试用以下代码将列表插入SQLite的articles表时:
using (var connection = new SQLiteConnection(System.IO.Path.Combine(apkFolder, "MobileSell.db"))) { connection.InsertAll(articles.AllArticles); }
抛出异常:
SQLite.SQLiteException: no such table: Article at SQLite.SQLite3.Prepare2
我的问题是:如何让程序识别Article对象对应articles表中的一行?
解决方案
这个问题的核心是你使用的SQLite-net库默认会把类名作为表名来查找——你的类叫Article,但数据库里的表是articles(复数形式),所以库找不到对应的表,才抛出了这个异常。解决起来很简单,只需要给Article类加上[Table]属性,明确指定它对应的数据库表名即可。
1. 添加Table属性映射表名
修改你的Article类,引入SQLite命名空间并添加[Table("articles")]属性(表名要和数据库里的完全一致,包括大小写,避免不必要的问题):
using SQLite; // 必须引入这个命名空间才能使用Table等属性 [XmlRoot(ElementName = "articlessql", Namespace = "http://tempuri.org/DataSet1.xsd")] [Table("articles")] // 关键:指定类对应的数据库表名 public class Article { [XmlElement(ElementName = "id", Namespace = "http://tempuri.org/DataSet1.xsd")] [PrimaryKey] // 如果id是表的主键,建议加上这个属性,帮助SQLite-net正确处理数据 public string Id { get; set; } [XmlElement(ElementName = "name", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Name { get; set; } [XmlElement(ElementName = "quantity", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Quantity { get; set; } [XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")] public string SellPrice { get; set; } [XmlElement(ElementName = "sellid", Namespace = "http://tempuri.org/DataSet1.xsd")] public string SellId { get; set; } [XmlElement(ElementName = "whole_price", Namespace = "http://tempuri.org/DataSet1.xsd")] public string WholePrice { get; set; } [XmlElement(ElementName = "group", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Group { get; set; } [XmlElement(ElementName = "barcode", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Barcode { get; set; } [XmlElement(ElementName = "measure", Namespace = "http://tempuri.org/DataSet1.xsd")] public string Measure { get; set; } }
2. 额外需要检查的点
- 确认数据库中的
articles表确实存在,并且表的字段结构和Article类的属性对应。如果字段名和属性名不一致(比如数据库字段是sell_price,类属性是SellPrice),可以给属性加上[Column("sell_price")]来完成映射。 - 确保你安装的SQLite-net包是正确的(比如
sqlite-net-pcl),并且项目中已经正确引用了该库。 - 如果你的数据库是预先创建的,要注意表名的大小写——虽然SQLite在多数系统上大小写不敏感,但严格匹配能避免潜在的兼容性问题。
完成以上修改后,再运行InsertAll方法,程序就能正确识别Article对象对应articles表的行,顺利插入数据了。
内容的提问来源于stack exchange,提问作者Profile2ForStack
相关产品推荐
相关产品推荐

