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 Data开发者_如何学CbaseHelper(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'=? "
You can change the id in ForeignData class into:
@DatabaseField(id = true)
private Integer id;
Another possibility (for other users that are having this same problem).
OrmLite lets your run in "configuration file mode" to help with performance. To check if you are using this mode, look at your OrmLiteSqliteOpenHelper class and see which version of the super constructor you are calling. If you're giving it a configFileId then you're in this mode.
Assuming that's the case, make sure you run your OrmLiteConfigUtil class as a Java Application to generate your configuration file. You need to do this EVERY time you make an ORM-related change to one of your model files because it uses this file as a source of truth for your models (it doesn't inspect the model files for annotations at runtime).
Try Uninstall the app from device, and run it again...it works....
I think there's something wrong with onUpgrade function on my databaseHelper, so the question might be no more relevant...
精彩评论