安卓/ ORMLite插入行与ID [英] Android/ORMLite Insert Row with ID

查看:610
本文介绍了安卓/ ORMLite插入行与ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用ORMLite与Android上的SQLite数据库。作为其中的一部分,我从后台服务器下载一组数据,我想有这些数据添加到SQLite数据库中完全一样的格式是在后端服务器上(即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).

所以,我想问你的是,如果我填充我的数据库条目对象(我们称之为IT设备),包括设备的generatedId /主键字段,通过SETID(),我再与运行DAO.create()设备条目的ID正确保存?我尝试了这种方式,并在我看来,这是情况并非如此。如果是这样的话,我会再次尝试,并寻找其他的问题,但与前几越过code我没能找到一个。所以基本上,如果我叫DAO.create()与ID集的数据库对象将是ID发送给数据库,如果不是这样,我怎么可以插入一排已经填写了一份主键值?

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?

谢谢!

推荐答案

@Femi是正确的对象可以是一个产生-ID的的一个ID,但的不是两者。问题是比它如何存储ORMLite对象,但也有相匹配的是与生成的数据库的模式。

@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 支持 allowGeneratedIdInsert = TRUE 选项 @ DatabaseField 批注,允许这种行为。这是不支持的一些数据库类型(德比为例),但在Android的/ SQLite的工作。

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.

有关后代,也可以创建2个对象共享同一个表 - 一个具有产生-ID,一个没有。然后,你可以插入使用生成的-ID道来获取行为和其他道取值id调用者设置。这里的<一个href="http://stackoverflow.com/questions/5216663/can-i-temporarily-suspend-auto-generated-id-in-ormlite">another回答说那。这个问题对你听起来,这将产生很多额外的DAO的。

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.

其他唯一的解决办法是的没有的使用id为您的目的。让数据库生成的ID,然后让您使用外部设置你的目的附加字段。强制数据库ID在某些情况下,在我看来是一个坏的模式。

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.

这篇关于安卓/ ORMLite插入行与ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆