Android/ORMLite 插入带 ID 的行

发布于 2024-11-29 12:01:30 字数 384 浏览 2 评论 0原文

我目前正在使用 ORMLite 来处理 Android 上的 SQLite 数据库。作为此过程的一部分,我从后端服务器下载一堆数据,我希望将这些数据以与后端服务器上完全相同的格式添加到 SQLite 数据库中(即 ID 相同等) 。

所以,我的问题是,如果我填充我的数据库条目对象(我们称之为设备),包括通过 setId() 的设备的 generatedId/主键字段,然后我使用该设备条目运行 DAO.create()该ID保存正确吗?我尝试了这种方式,但在我看来,情况并非如此。如果是这种情况,我会再试一次并寻找其他问题,但在前几次检查代码时我无法找到问题。因此,本质上,如果我在具有 ID 集的数据库对象上调用 DAO.create() ,该 ID 是否会发送到数据库,如果不是,我如何插入主键值已填写的行?

谢谢!

I'm currently using ORMLite to work with a SQLite database on Android. As part of this I am downloading a bunch of data from a backend server and I'd like to have this data added to the SQLite database in the exact same format it is on the backend server (ie the IDs are the same, etc).

So, my question to you is if I populate my database entry object (we'll call it Equipment), including Equipment's generatedId/primary key field via setId(), and I then run a DAO.create() with that Equipment entry will that ID be saved correctly? I tried it this way and it seems to me that this was not the case. If that is the case I will try again and look for other problems, but with the first few passes over the code I was not able to find one. So essentially, if I call DAO.create() on a database object with an ID set will that ID be sent to the database and if it is not, how can I insert a row with a primary key value already filled out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-12-06 12:01:30

@Femi 是正确的,对象可以是生成的 id 或 id,但不能两者兼而有之。问题不仅仅是 ORMLite 如何存储对象,而且它还必须与生成数据库的模式相匹配。

ORMLite 支持 @DatabaseField 注释的 allowGenerateIdInsert=true 选项允许这种行为。某些数据库类型(例如 Derby)不支持此功能,但可以在 Android/SQLite 下使用。

为了后代,您还可以创建 2 个共享同一个表的对象——一个具有 generated-id,另一个没有。然后,您可以使用 generated-id Dao 进行插入以获取该行为,并使用另一个 Dao 来获取调用者设置的 id 值。这是另一个关于这个问题的答案。对您来说,问题似乎是这会创建大量额外的 DAO。

唯一的其他解决方案是将 id 用于您的目的。让数据库生成 id,然后有一个您使用的附加字段,该字段是根据您的目的在外部设置的。在我看来,在某些情况下强制使用数据库 ID 是一个不好的模式。

@Femi is correct that an object can either be a generated-id or an id, but not both. The issue is more than how ORMLite stores the object but it also has to match the schema that the database was generated with.

ORMLite supports a allowGeneratedIdInsert=true option to @DatabaseField annotation that allows this behavior. This is not supported by some database types (Derby for example) but works under Android/SQLite.

For posterity, you can also create 2 objects that share the same table -- one with a generated-id and one without. Then you can insert using the generated-id Dao to get that behavior and the other Dao to take the id value set by the caller. Here's another answer talking about that. The issue for you sounds like that this will create a lot of of extra DAOs.

The only other solution is to not use the id for your purposes. Let the database generate the id and then have an additional field that you use that is set externally for your purposes. Forcing the database-id in certain circumstances seems to me to be a bad pattern.

江南月 2024-12-06 12:01:30

来自 http://ormlite.com/docs/ generated-id

布尔值,该字段是否是自动生成的 id 字段。默认为 false。一个类中只有一个字段可以有此设置。这告诉数据库为插入的每一行自动生成相应的 id。当使用 Dao.create() 方法创建具有 generated-id 的对象时,数据库将为该行生成一个 id,该 id 将通过 create 方法返回并设置在对象中。某些数据库需要生成 ID 的序列,在这种情况下,序列名称将自动生成。要指定序列的名称,请使用 generatedIdSequence。只能指定 this、id 和 generatedIdSequence 之一。

您必须使用 generateId (在这种情况下,必须生成所有 id)或 id (在这种情况下您可以设置它们),但不能同时使用两者。

From http://ormlite.com/docs/generated-id:

Boolean whether the field is an auto-generated id field. Default is false. Only one field can have this set in a class. This tells the database to auto-generate a corresponding id for every row inserted. When an object with a generated-id is created using the Dao.create() method, the database will generate an id for the row which will be returned and set in the object by the create method. Some databases require sequences for generated ids in which case the sequence name will be auto-generated. To specify the name of the sequence use generatedIdSequence. Only one of this, id, and generatedIdSequence can be specified.

You must use either generatedId (in which case it appears all ids must be generated) or id (in which case you can set them) but not both.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文