Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite can't do queryForId

I'm new to android and ormLite and I just try to store data and get it back again using queryForId. At first it work well, but somehow after fiddling a bit...I got exception that says my class don't have an id field.

here's the code for the class

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

  private static final long serialVersionUID = 3640428963380696279L;

  @DatabaseFieldId(generatedId = true)
  private Integer id;

  @DatabaseFieldSimple(defaultValue = "")
  private String name;

  public ForeignData(){};
}

and this is how I call it...

ForeignData f = new ForeignData();
try {
    Dao<ForeignData, Integer> fdao = new DatabaseHelper(getApplicationContext()).getForeignDao();
    f.setName("test");
    fdao.create(f);
    f = fdao.queryForId(f.getId())==null?f:fdao.queryForId(f.getId());
    txt1.setText(f.getName());                  
} catch (SQLException e) {
    txt1.setText(e.getMessage());
}

and it catch exception at

f = fdao.queryForId(f.getId())==null?f:fdao.queryForId(f.getId());

which return cannot query-for-id because ForeignData don't have an id column.

and if I change the id in ForeignData class into

 @DatabaseField(generatedId=true,columnName="id")
 private Integer id;

The exception I get is "queryForOne from database field: SELECT * FROM 'ForeignData' WHERE 'id'=? "

like image 837
Ridwan Yusuf Avatar asked Jun 24 '26 01:06

Ridwan Yusuf


1 Answers

You can change the id in ForeignData class into:

@DatabaseField(id = true)
private Integer id;
like image 66
lightspeed Avatar answered Jun 26 '26 15:06

lightspeed