【发布时间】:2013-03-18 11:47:25
【问题描述】:
带有 WP8 的 SQLite 让我抓狂。 :(
我要做的就是检索最后插入的 id 的值...
我有:
class ShoppingItem
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
public string Shop {get; set;}
public bool isActive {get; set;}
}
嗯,使用的 SQLiteConnection 对象和它的 Table<ShoppingItem> 似乎都没有包含包含最后一个 ID 的适当成员。
所以我尝试这样做:
private int GetLastInsertedRowID()
{
int Result = -1;
using (var db = new SQLiteConnection(m_DatabasePath)) {
Result = db.ExecuteScalar<int>("SELECT last_insert_rowid();");
}
return Result;
}
但是这个函数总是返回0。 :(
但是当我阅读ShoppingItem 的所有条目时,它们的 ID 的值是 != 0。
所以我的问题是:如何检索最后插入的 id?
PS:将 SQL 查询更改为 SELECT last_insert_rowid() FROM ShoppingItem; 得到相同的结果。
PPS:Getting the Last Insert ID with SQLite.NET in C# 之类的解决方案无法编译,显然使用的是旧版本的 SQLite,其 API 完全不同
【问题讨论】:
-
是 db.ExecuteScalar
("SELECT last_insert_rowid()");在职的? (没有 ; 在查询的末尾) -
不,有或没有分号的结果相同:(
-
我建议您为您的 pk 列“Id”选择一个不同的名称。我不知道 Id 是否类似于内部关键字。但我只是在这里猜测。
-
不,将其更改为 myId。 :(
标签: c# sqlite windows-phone-8