【发布时间】:2013-03-06 14:47:52
【问题描述】:
我尝试使用 WP8 的 SQLite 库。
我定义了一个表:
class ShoppingItem
{
[SQLite.PrimaryKey]
public int Id {get; set;}
public string Name {get; set;}
public string Shop {get; set;}
public bool isActive {get; set;}
}
根据http://www.sqlite.org/autoinc.html,如果没有给Id 赋值,sqlite 会自动为Id 赋值。对吧?
所以我尝试通过
插入新条目using (var db = new SQLiteConnection(m_DatabasePath)) {
db.Insert(new ShoppingItem() {
Name = anItem,
Shop = aShop,
isActive = aIsActive,
});
}
当我插入第一个项目时,它的 ID 为 0。好吧,为什么不呢。 当我插入第二个项目时,我得到一个带有令人敬畏的消息“约束”的 SQLiteException。 那么如何在不提供 id 的情况下插入新条目呢?
顺便说一句: 作为替代解决方案,我尝试将空值添加到 id,但这会导致编译错误。 :(
...
db.Insert(new ShoppingItem() {
Id = null, // <- does not compile
Name = anItem,
...
【问题讨论】:
-
您的 ID 是数据库表中的自动增量吗?
-
不,但我认为没有必要,根据sqlite.org/autoinc.html
标签: c# sqlite windows-phone-8 auto-increment