gpt4 book ai didi

android - 如何在没有 ID 的情况下创建新对象并将其插入到我的数据库中?

转载 作者:搜寻专家 更新时间:2023-11-01 08:06:54 25 4
gpt4 key购买 nike

我们的团队对创建新的数据库对象有些困惑。

如果创建了 idproperty,greendao orm 生成器将创建一个标准构造函数,该构造函数将 id 作为第一个值。对于我们的大多数数据库,我们不想自己创建 id,而只是让数据库创建 id。

目前大多数团队成员将 -1L 作为 id 传递。这似乎有效。

我找不到在没有 ID 的情况下创建新对象的记录方法。

创建这样一个对象的正确方法是什么?在这种情况下,是在插入数据库的过程中生成一个新的 id 吗?

最佳答案

快速浏览一下 docs for Greendao揭示:

Now have a look at the addNote method, how you insert a new note in the database:

Note note = new Note(null, noteText, comment, new Date());
noteDao.insert(note);
Log.d("DaoExample", "Inserted new note, ID: " + note.getId());

Just create a Java object and call insert on the DAO. When the insert method returns, the database id of the just inserted note is already assigned to the object, as you can see in the log statment.

看起来 null 是您想要的答案。

关于android - 如何在没有 ID 的情况下创建新对象并将其插入到我的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com