对于我目前的项目,我决定使用ORMLite(4.46)。目前,我在保存异物数据时遇到了一些问题。下面是我的代码示例。称为“字段”的字段未正确保存(显示旧数据)。任何人都有想法,这段代码有什么问题?
谢谢!
此致 安德烈亚斯
某些课程:
DatabaseHelper databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class)
branch.field = 123423;
Log.d("...", "field:" + branch.field); <-- new value of field is displayed
databaseHelper.getBranchDao().update(branch);
其他课程:
DatabaseHelper databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
databaseHelper.getFloorDao().refresh(floor);
databaseHelper.getBranchDao().refresh(floor.branch);
Log.d("...", "branch field:" + floor.branch.field); <-- old value of field is displayed
DatabaseHelper:
...
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// name of the database file for your application -- change to something appropriate for your app
private static final String DATABASE_NAME = "xy.db";
// any time you make changes to your database objects, you may have to increase the database version
private static final int DATABASE_VERSION = 1;
// the DAO objects we use to access the tables
private Dao<Branch, Integer> branchDao = null;
private Dao<Floor, Integer> floorDao = null;
private Context appContext;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
appContext = context;
}
/**
* This is called when the database is first created. Usually you should call createTable statements here to create
* the tables that will store your data.
*/
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
// Create tables for each domain object here!
TableUtils.createTable(connectionSource, Branch.class);
TableUtils.createTable(connectionSource, Floor.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
/**
* This is called when your application is upgraded and it has a higher version number. This allows you to adjust
* the various data to match the new version number.
*/
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
// Drop all tables of domain objects here!
TableUtils.dropTable(connectionSource, Branch.class, true);
TableUtils.dropTable(connectionSource, Floor.class, true);
// after we drop the old databases, we create the new ones
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
/**
* Delete and re-create database when user is changed
*/
public void reCreateDB(){
//delete the database (onCreate() will be called if database is needed again)
appContext.deleteDatabase(DATABASE_NAME);
}
/**
* Returns the Database Access Object (DAO) for our domain class. It will create it or just give the cached
* value.
*/
public Dao<Branch, Integer> getBranchDao() throws SQLException {
if (branchDao == null) {
branchDao = getDao(Branch.class);
}
return branchDao;
}
public Dao<Floor, Integer> getFloorDao() throws SQLException {
if (floorDao == null) {
floorDao = getDao(Floor.class);
}
return floorDao;
}
/**
* Close the database connections and clear any cached DAOs.
*/
@Override
public void close() {
super.close();
}
}
楼层:
...
public class Floor {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(foreign = true, canBeNull = true, foreignAutoRefresh = true, maxForeignAutoRefreshLevel=5)
public Branch branch;
public Floor(){}
}
科:
....
public class Branch {
@DatabaseField(generatedId = true)
public int id;
@DatabaseField(canBeNull = true)
public int field;
@ForeignCollectionField(eager = true, maxEagerLevel = 99)
private ForeignCollection<Floor> floors;
public Branch(){}
public ForeignCollection<Floor> getEmptyFloorCollection() {
try {
return MainActivity.getDatabaseHelper().getBranchDao().getEmptyForeignCollection("floors");
} catch (SQLException e) {
Log.e("DatabaseHelper", "SQL Exception: " + e.getLocalizedMessage());
return null;
}
}
public ForeignCollection<Floor> getFloors() {
if(floors == null) floors = getEmptyFloorCollection();
return floors;
}
public void setFloors(ForeignCollection<Floor> floors) {
this.floors = floors;
}
}