c# –Xamarin.Android SQLite SQLiteConnection.Insert不返回插入对象的ID

c# –Xamarin.Android SQLite SQLiteConnection.Insert不返回插入对象的ID,第1张

概述我是Xamarin和C#的新手,并尝试将一些示例数据插入到我的简单配方数据库中以测试purpourses.但是,在插入新配方时,SQLiteConnection.Insert(数据)不会返回插入记录的ID(请参阅here),而是每次都返回1.我的插入数据方法:publicstaticintinsertUpdate(Objectdata){str

我是Xamarin和C#的新手,并尝试将一些示例数据插入到我的简单配方数据库中以测试purpourses.

但是,在插入新配方时,sqliteConnection.Insert(数据)不会返回插入记录的ID(请参阅here),而是每次都返回1.

我的插入数据方法:

public static int insertUpdate(Object data) {        string path = DB.pathToDatabase ();        DB.createDatabase ();        try {            var db = new sqliteConnection(path);            int inserted = db.Insert(data);            if (inserted != 0)                inserted = db.Update(data);            return inserted;        }        catch (sqliteException ex) {            return -1;        }    }

插入样本数据:

        int stand = insertUpdate (new Recipe {            name = "Standard",            Author = "Aeropress Nerd",            Description = "The perfect recipe for your first cup",            Type = "Default"        });        insertUpdate (new Step { Action = "Pour", Duration = 10, Recipe = stand });        insertUpdate (new Step { Action = "Stir", Duration = 20, Recipe = stand });        insertUpdate (new Step { Action = "Steep", Duration = 15, Recipe = stand });        int inv = insertUpdate (new Recipe {            name = "Inverted",            Author = "Aeropress Nerd",            Description = "A more advanced brew using the inverted method.",            Type = "Default"        });

我无法弄清楚我做错了什么.在此先感谢任何帮助,并抱歉(可能)愚蠢的问题.

解决方法:

However, when inserting a new recipe, the sqliteConnection.Insert(data) does not return the ID of the inserted record as it should (see here), but instead returns 1 every time.

我很确定你下载了一些nuget包或组件来包含sqlite功能到你的Xamarin.AndroID应用程序,它可能与本机实现略有不同.然后,您应该参考特定文档,了解您在Xamarin上使用的任何内容.

我的猜测是你正在使用this component.如果我错了,请评论以纠正我的答案.如果我是对的,你应该试试这个:

要插入的对象

class Row{   public int ID { get; set; }}class Recipe : Row{    //Recipe's propertIEs}class Step : Row{    //Step's propertIEs}

insertUpdate方法定义

public static int insertUpdate(Row data) {    string path = DB.pathToDatabase ();    DB.createDatabase ();    try {        var db = new sqliteConnection(path);        int inserted = db.Insert(data); //will be 1 if successful        if (inserted > 0)            return data.ID; //Acording to the documentation for the sqlIte component, the Insert method updates the ID by reference        return inserted;    }    catch (sqliteException ex) {        return -1;    }}

insertUpdate方法用法

 //As Step and Recipe both are derived classed from Row, you should be able to use insertUpdate indistinctively and without casting    insertUpdate (new Step { Action = "Steep", Duration = 15, Recipe = stand });    int inv = insertUpdate (new Recipe {        name = "Inverted",        Author = "Aeropress Nerd",        Description = "A more advanced brew using the inverted method.",        Type = "Default"    });

总结

以上是内存溢出为你收集整理的c# – Xamarin.Android SQLite SQLiteConnection.Insert不返回插入对象的ID全部内容,希望文章能够帮你解决c# – Xamarin.Android SQLite SQLiteConnection.Insert不返回插入对象的ID所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1108143.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存