Android ORMlite - 多个表/类
我在使用具有多个表/类的 ORMlite 框架时遇到问题。
我有一个 DatabaseHelper.class,它扩展了 OrmLiteSqliteOpenHelper,它看起来像这样(重要部分):
public class DatabaseHelper<T> extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 6;
private Dao<T, Integer> myDao = null;
private Class<T> type;
public DatabaseHelper(Context context, Class<T> type) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.type = type;
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, type);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Dao<T, Integer> getmyDataDao() throws SQLException {
if (myDao == null) {
myDao = getDao(type);
}
return myDao;
}
}
我使用 2 个类(Admin1、Admin2),我想在数据库中保存 2 个不同的对象(Object1、Object2)。这是我用于此目的的代码。
Amin1:
static {
OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
public OrmLiteSqliteOpenHelper getHelper(Context context) {
return new DatabaseHelper<Object1>(context, Object1.class);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.object1);
Dao<Object1, Integer> myDaoObject1 = getHelper().getmyDataDao();
Object1 o1 = new Object1("Name of Object 1");
int ret = myDaoObject1.create(o1);
}
Admin2:
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);
}
对于我保存的第一个对象(在 Admin1 或 Admin2 中),它工作正常。但是,如果我将 object1 保存在 Admin1 中,然后我想将 object2 保存在 Admin2 中或相反(顺序不是决定性的),我会在“int ret = myDaoObject2.create(o2);”行中收到以下错误
Unable to run stmt on object com.example.Object2@44c408f0: INSERT INTO `object2` (`name` ) VALUES (?)
:在代码中创建 Object2 o2 时出错。不在“int ret = myDaoObject2.create(o2);”行之前发生错误...我不知道为什么有一个“?”在价值中。对象前面的一行看起来不错。
有人有想法吗?
多谢!
更新
不幸的是我无法解决这个问题。
我现在尝试在活动的 onPause()
和/或 onStop()
处关闭 OrmLiteSqliteOpenHelper:
myHelper.close();
从那时起,我收到错误:
“无法销毁活动 - 空指针异常”
我无法理解这个错误。
有人有想法吗?
I have a problem by using the ORMlite framework with more than one table / classes.
I´ve got one DatabaseHelper.class which extends the OrmLiteSqliteOpenHelper which looks like this (important parts):
public class DatabaseHelper<T> extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 6;
private Dao<T, Integer> myDao = null;
private Class<T> type;
public DatabaseHelper(Context context, Class<T> type) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.type = type;
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, type);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Dao<T, Integer> getmyDataDao() throws SQLException {
if (myDao == null) {
myDao = getDao(type);
}
return myDao;
}
}
I use 2 classes (Admin1, Admin2) in which I want to save 2 different objects (Object1, Object2) in the database. Here the code I used for this.
Amin1:
static {
OpenHelperManager.setOpenHelperFactory(new SqliteOpenHelperFactory() {
public OrmLiteSqliteOpenHelper getHelper(Context context) {
return new DatabaseHelper<Object1>(context, Object1.class);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.object1);
Dao<Object1, Integer> myDaoObject1 = getHelper().getmyDataDao();
Object1 o1 = new Object1("Name of Object 1");
int ret = myDaoObject1.create(o1);
}
Admin2:
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);
}
It works fine for the first object I save (in Admin1 or Admin2). But if I save object1 in Admin1 and after that I want to save object2 in Admin2 or reverse (the order is not decisive), I get following error in the line "int ret = myDaoObject2.create(o2);":
Unable to run stmt on object com.example.Object2@44c408f0: INSERT INTO `object2` (`name` ) VALUES (?)
There is no error in creating the Object2 o2 in the code. Not before the line "int ret = myDaoObject2.create(o2);" the error occurs ... I have no idea why there is an "?" in the value. One line before the object looks fine.
Has anybody an idea?
Thanks a lot!
Update
Unfortunately I couldn`t solve the problem.
I´ve tried now to close the OrmLiteSqliteOpenHelper at the onPause()
and/or at the onStop()
of the activitiy:
myHelper.close();
Since then I get the error:
"Unable to destroy activity - null pointer exception"
I can't understand this error.
Does anybody have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的DB文件不是在admin2初始化时完全重写的吗?
您真的需要两个不同的 Helper 实例吗?如果您只需要为它们提供两个不同的 dao 对象 - 使用一个共享帮助器并添加一个新的 getAdmin2Dao 方法。
HTH
编辑(添加一些代码):
Isn't your DB file completely rewritten on admin2 initialization?
Do you really need two different Helper instances? If you just need to have two different dao objects for them - use one shared helper and just add a new getAdmin2Dao method.
HTH
EDIT (Adding some code):
我两天前收到这个错误,今晚我刚刚解决了:)当您创建第一个对象时,您可以执行任何数据库操作,但是当您想创建另一个对象并执行一些操作(例如添加或删除等)时..您得到错误,因为数据库助手不会创建第二个对象(TAble)。首先,当您打开应用程序时,您必须创建对象(TAble 类),如下所示
I got this error 2 days ago and I just solved this night :) When you create your first Object, You can do any database operations but when you want to create another object and doing some operations like add or delete or etc..you get an error because Database helper Doesn't create second object(TAble).Firstly, when you open your Application you have to create your objects(TAble classes) like below