Android ormlite 更新序列化对象中的行

标签 android ormlite

我有 3 张 table :

@DatabaseTable(tableName="user")
public class TableUser  implements Serializable{

  @DatabaseField(dataType = DataType.SERIALIZABLE)
    private LinkedList<TableProfile> profiles;
}

@DatabaseTable(tableName="profile")
public class TableProfile implements Serializable{

   @DatabaseField(dataType = DataType.SERIALIZABLE)
    private LinkedList<TableRevel> revelations;
}

@DatabaseTable(tableName="revel")
public class TableRevel implements Serializable{
    private String our_profile_id;
    @DatabaseField(canBeNull=false)
    private String other_profile_id;
    @DatabaseField(canBeNull=false)
    private String info;
    @DatabaseField(canBeNull=false)
}

我需要更新“revel”表中的文件,我从没有对象“user:{profile:...}”的 Json 中获得启示

我想我需要使用 QueryBuilder,如果可以的话请写出这个查询的简短示例。

最佳答案

我会强制列名称为静态名称,以便您可以对其进行搜索。像这样的事情:

@DatabaseTable(tableName="revel")
public class TableRevel implements Serializable{
  public static final String PROFILE_ID = 'id';
  @DatabaseField(canBeNull=false, columnName = PROFILE_ID)
  private String our_profile_id;
  @DatabaseField(canBeNull=false)
  private String other_profile_id;
  @DatabaseField(canBeNull=false)
  private String info;
}

然后我会做这样的事情来进行更新:

public class foo extends OrmLiteSqliteOpenHelper {
...

public updateField(String jsonString) throws SQLException{
  Dao<TableRevel, String> revelDAO = getDao(TableRevel.class);
  QueryBuilder<TableRevel, String> queryBuilder = revelDAO.queryBuilder();
  queryBuilder.where().eq(TableRevel.PROFILE_ID, getIdFromJson(jsonString) );
  PreparedQuery<TableRevel> preparedQuery = queryBuilder.prepare();
  TableRevel revelEntry = revelDAO.queryForFirst();

  //update the entry here
  ...

  revelDAO.update(revelEntry); //update the data

我编写了一个使用 OrmLite 的项目。执行大量 sql 操作的类的链接如下:https://github.com/kopysoft/Chronos/blob/development/ChronosApp/src/com/kopysoft/chronos/content/Chronos.java

希望这有帮助!

关于Android ormlite 更新序列化对象中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9741200/

相关文章:

java - 在 logcat 中显示数组值

android - Sqlite 数据库应该只创建一次

android - 如何为 AndroidAnnotations 4.0.0 调整 AndroidAnnotations 3.x 兼容的 ORMLite 代码

mysql - 如何在@DatabaseTable中引用不同的模式

mysql - SQLException : Access denied when connecting to mysql in docker

java - 安卓java : How to send message from server to a specific client any time

android - 异步任务内存泄漏

android - 如何在现有 Activity 中加载另一个 Layout xml 文件?

android - 从 ForeignCollection 添加、删除对象

android - ORMLite - 查询外域