Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin Sqlite插入不返回最后一个主键id_Sqlite_Xamarin_Android Sqlite_Sqlite Net - Fatal编程技术网

Xamarin Sqlite插入不返回最后一个主键id

Xamarin Sqlite插入不返回最后一个主键id,sqlite,xamarin,android-sqlite,sqlite-net,Sqlite,Xamarin,Android Sqlite,Sqlite Net,我有一个简单的实体: [Table("History")] public class History { [PrimaryKey, AutoIncrement, Column("_id")] public int Id { get; set; } [Indexed(Name = "IDX_History", Order = 1, Unique = true)] public int Prefix { get; set; } [Indexed(Name

我有一个简单的实体:

[Table("History")]
public class History
{
    [PrimaryKey, AutoIncrement, Column("_id")]
    public int Id { get; set; }

    [Indexed(Name = "IDX_History", Order = 1, Unique = true)]
    public int Prefix { get; set; }

    [Indexed(Name = "IDX_History", Order = 2, Unique = true)]
    public int Stem { get; set; }

    [Indexed(Name = "IDX_History", Order = 3, Unique = true)]
    public int Suffix { get; set; }

    [Indexed(Name = "IDX_Favourite")]
    public bool IsFavourite { get; set; }

    public DateTime LastViewed { get; set; } = DateTime.Now;
}
我正在尝试进行插入(如果是新的),或者尝试获取上次插入的id(如果已存在):

public static int SaveSolutionToHistory(Solution sol)
{
    lock (_db)
    {
        var existingHistory = _db.Table<History>().FirstOrDefault(h => h.Prefix == sol.Prefix.Id 
            && h.Stem == sol.Stem.Id 
            && h.Suffix == sol.Suffix.Id);

        if (existingHistory != null)
        {
            existingHistory.LastViewed = DateTime.Now;
            _db.Update(existingHistory);
            return existingHistory.Id;
        }

         _db.Insert(new History
        {
            Prefix = sol.Prefix.Id,
            Stem = sol.Stem.Id,
            Suffix = sol.Suffix.Id
        });

        return _db.ExecuteScalar<int>("SELECT last_insert_rowid()");
    }
}
这将返回正确的id:

return _db.ExecuteScalar<int>("SELECT last_insert_rowid()");
return _db.ExecuteScalar(“选择最后一次插入\u rowid()”);
对我来说,像这样做两次点击或者以非强类型的方式进行点击似乎效率低下。
\u db.Insert
没有返回正确Id的原因是什么


我使用的是VS 2015,带有HAXM和Marshmallow x86_64谷歌API图像。

我也有同样的问题,刚刚找到答案,完整的xml注释是:

//
// Summary:
//     /// Inserts the given object and retrieves its /// auto incremented primary key
//     if it has one. ///
//
// Parameters:
//   obj:
//     /// The object to insert. ///
//
// Returns:
//     /// The number of rows added to the table. ///
摘要“检索”的意思是更新传递给它的对象中PK字段的值,因此如果需要最后插入的id,可以使用该字段

可以使用insert在数据库中插入行。如果桌子 包含自动递增的主键,然后是该键的值 将在插入后提供给您:

这意味着ID将在对象字段ID中可用


哇。谢谢你!我一直在追根究底,因为我没有完全阅读文档,只是看到“检索”这个词,我想这就是它要返回的东西!
//
// Summary:
//     /// Inserts the given object and retrieves its /// auto incremented primary key
//     if it has one. ///
//
// Parameters:
//   obj:
//     /// The object to insert. ///
//
// Returns:
//     /// The number of rows added to the table. ///
public static void AddStock (SQLiteConnection db, string symbol) {
    var s = new Stock { Symbol = symbol };
    db.Insert (s);
    Console.WriteLine ("{0} == {1}", s.Symbol, s.Id);
}