Android ORMlite-多个表/类
我在使用ORMlite框架时遇到了一个问题,它包含多个表/类 我有一个DatabaseHelper.class,它扩展了OrmLiteSqliteOpenHelper,如下所示(重要部分): 在代码中创建Object2 o2时没有错误。不在“int ret=myDaoObject2.create(o2);”行之前发生错误。。。我不知道为什么值中有一个“?”。对象前的一行看起来很好 有人有主意吗 非常感谢 更新 不幸的是,我不能解决这个问题 我现在已尝试在活动的Android ORMlite-多个表/类,android,ormlite,Android,Ormlite,我在使用ORMlite框架时遇到了一个问题,它包含多个表/类 我有一个DatabaseHelper.class,它扩展了OrmLiteSqliteOpenHelper,如下所示(重要部分): 在代码中创建Object2 o2时没有错误。不在“int ret=myDaoObject2.create(o2);”行之前发生错误。。。我不知道为什么值中有一个“?”。对象前的一行看起来很好 有人有主意吗 非常感谢 更新 不幸的是,我不能解决这个问题 我现在已尝试在活动的onPause()和/或onStop
onPause()
和/或onStop()
上关闭OrmLiteSqliteOpenHelper:
myHelper.close();
从那以后,我得到了一个错误:
“无法销毁活动-空指针异常”
我不能理解这个错误
有人知道吗?在admin2初始化时,您的DB文件不是被完全重写了吗? 您真的需要两个不同的助手实例吗?如果您只需要为它们提供两个不同的dao对象,请使用一个共享帮助器并添加一个新的getAdmin2Dao方法 嗯 编辑(添加一些代码):
private-Dao-profileDao=null;
私有Dao网络Dao=null;
@凌驾
public void onCreate(SQLiteDatabase db,ConnectionSource ConnectionSource){
试一试{
Log.i(DBHelper.class.getName(),“onCreate”);
createTable(connectionSource,Network.class);
createTable(connectionSource,Profile.class);
}捕获(SQLE异常){
Log.e(DBHelper.class.getName(),“无法创建数据库”,e);
抛出新的运行时异常(e);
}
}
公共Dao getProfileDao()引发SQLException{
if(profileDao==null){
profileDao=getDao(Profile.class);
}
返回profileDao;
}
// ... 另一把刀也一样
@凌驾
公众假期结束(){
super.close();
networkDao=null;
profileDao=null;
}
我两天前遇到了这个错误,我今天晚上刚刚解决了:)当您创建第一个对象时,您可以执行任何数据库操作,但当您要创建另一个对象并执行一些操作(如添加或删除等)时,您会遇到一个错误,因为数据库帮助程序没有创建第二个对象(表)。首先,打开应用程序时,必须创建对象(表类),如下所示
TableUtils.createTable(helper.getConnectionSource(), BudgetCategory.class);
....
....
我不是舒尔,不管我是否需要两个不同的助手实例。我只需要在不同的操作中从数据库的不同表中获取数据(对象)。。。不幸的是,我不知道最好的做法。你能给我一些更多的细节或者你提示的示例代码吗?thnx!这很有帮助。我的第一个意图是用泛型解决这个问题,但我认为这也没关系!我要用它!谢谢!;)这是亚历克斯的现场谢谢@SKAf8ce您肯定只有一个助手和一个到数据库的连接。如果您查看Android ORMLite示例,您可以看到它们都使用相同的帮助器:请编辑您的问题以提供附加信息,或者使用单个答案下的注释工具来要求澄清。
static {
OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
public OrmLiteSqliteOpenHelper getHelper(Context context) {
return new DatabaseHelper<Object2>(context, Object2.class);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.object2);
Dao<Object2, Integer> myDaoObject2 = getHelper().getmyDataDao();
Object2 o2 = new Object2("Name of Object 2");
int ret = myDaoObject2.create(o2);
}
Unable to run stmt on object com.example.Object2@44c408f0: INSERT INTO `object2` (`name` ) VALUES (?)
myHelper.close();
private Dao<Profile, String> profileDao = null;
private Dao<Network, String> networkDao = null;
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DBHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Network.class);
TableUtils.createTable(connectionSource, Profile.class);
} catch (SQLException e) {
Log.e(DBHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
public Dao<Profile, String> getProfileDao() throws SQLException {
if (profileDao == null) {
profileDao = getDao(Profile.class);
}
return profileDao;
}
// ... Same for the other DAO
@Override
public void close() {
super.close();
networkDao = null;
profileDao = null;
}
TableUtils.createTable(helper.getConnectionSource(), BudgetCategory.class);
....
....