Android/ORMLite 插入带 ID 的行
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@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.
来自 http://ormlite.com/docs/ generated-id:
您必须使用
generateId
(在这种情况下,必须生成所有 id)或id
(在这种情况下您可以设置它们),但不能同时使用两者。From http://ormlite.com/docs/generated-id:
You must use either
generatedId
(in which case it appears all ids must be generated) orid
(in which case you can set them) but not both.