我在表description
中添加了一列(account
)。我还阅读了this guild来升级我的数据库。但是,我对此代码中的getHelper()
方法有点混淆:
Dao<Account, Integer> dao = getHelper().getAccountDao();
// change the table to add a new column named "description"
dao.executeRaw("ALTER TABLE `account` ADD COLUMN description INTEGER;");
它来自哪里?我没有在我的getHelper()
课程中看到DatabaseHelper
被宣布。有人能帮助我吗?
答案 0 :(得分:0)
您应该在OrmLiteSqliteOpenHelper
创建表格的地方onCreate
,并更新它们(在onUpgrade
中):
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "database.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Account.class);
} catch (SQLException e) {
throw new RuntimeException("Error when create database tables", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
database.execSQL("ALTER TABLE `account` ADD COLUMN description INTEGER;");
//same as:
//AccountDAO dao = getDao(Acount.class);
//dao.executeRaw("ALTER TABLE 'account' ADD COLUMN description INTEGER;");
}
}
OrmLiteBaseListActivity
,OrmLiteBaseService
和OrmLiteBaseTabActivity
提供了一种方法getHelper
来访问。{1}} 数据库帮助程序,只要需要它并将自动创建 onCreate()方法中的助手,并在onDestroy()中释放它 方法
此外,如果你使用上面的类,你应该有这样的东西:
public class MyACtivity extends OrmLiteBaseActivity<DatabaseHelper> {
//In this class, you can call getHelper() to obtain the DatabaseHelper instance
}