Igor Karun
Igor Karun

Reputation: 629

Android ormlite update rows in Serialized Object

I have 3 tables:

@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)
}

I need update fileds in "revel" table , I get revelations from Json without object "user:{profile:...}"

I think I need use QueryBuilder, please write short example of this query if its passible.

Upvotes: 2

Views: 3559

Answers (2)

Ethan
Ethan

Reputation: 6913

I would force the column name to be something static so you can search on it. Something like this:

@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;
}

Then I would do something like this to do the update:

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

I have written a project that uses OrmLite. The link to the class that does a lot of the sql stuff is here: https://github.com/kopysoft/Chronos/blob/development/ChronosApp/src/com/kopysoft/chronos/content/Chronos.java

Hope this helps!

Upvotes: 1

AgentKnopf
AgentKnopf

Reputation: 4345

As I understand Ethan's answer it would mean loading the TableRevel object into memory in order to update it. Depending on the type of update you need, this might be a viable alternative:

public update(String jsonString) throws SQLException{

  Dao<TableRevel, String> dao= getDao(TableRevel.class);
  UpdateBuilder<TableRevel, String> updateBuilder = dao.updateBuilder();
  //Update the columns you want to update for the given jsonString-match
  updateBuilder.updateColumnValue(TableRevel.My_FIELD, newValue);
  // ...where the json string matches
  updateBuilder.where().eq(TableRevel.PROFILE_ID, getIdFromJson(jsonString));
  //Execute the update
  dao.update(updateBuilder.prepare());

}

Sorry if I misunderstood your question, but the info you provide is a bit sparse and I for once have no idea about the internals of json ;) . - either way this update query might suit your needs, depending on how complex your udpate is. Big plus: No need to load the object into memory.

Upvotes: 1

Related Questions