升级app / database版本后,从ormlite数据库保留数据

时间:2017-09-22 13:47:13

标签: java android database upgrade ormlite

每次升级数据库/应用版本时,我都会删除所有来自favoritesActivity的数据。升级应用程序后如何保留数据?现在我在我的DatabaseHelper中的onUpgrade方法中删除表,但是我需要在更新后保留数据。我正在使用ormlite框架。

编辑:

MainActivity(我在LongClick上添加数据)

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.listViewSample)
    ListView listViewSample;
    @BindView(R.id.nextBtn)
    Button nextBtn;

    DatabaseHelper databaseHelper;

    List<Ingredient> data;

    @OnClick(R.id.nextBtn)
    public void next(){
        Intent i = new Intent(MainActivity.this,FavoriteActivity.class);
        startActivity(i);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        data = new ArrayList<>();
        Ingredient i1 = new Ingredient(1,"banana1");
        Ingredient i2 = new Ingredient(2,"banana2");
        Ingredient i3 = new Ingredient(3,"banana3");
        Ingredient i4 = new Ingredient(4,"banana4");
        data.add(i1);
        data.add(i2);
        data.add(i3);
        data.add(i4);
        CustomAdapter adapter = new CustomAdapter(getApplicationContext(),R.layout.row_item,data);
        listViewSample.setAdapter(adapter);

        listViewSample.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                try {
                    Dao<Ingredient,Integer> ingredientDao = getHelper().getIntegerDao();
                    Ingredient i = new Ingredient(data.get(position).getId(),data.get(position).getName());
                    ingredientDao.create(i);
                    Toast.makeText(getApplicationContext(),"Dodałeś: " +i.getName()+" do ulubionych",Toast.LENGTH_SHORT).show();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
    }

    private DatabaseHelper getHelper(){
        if(databaseHelper==null)
            databaseHelper = OpenHelperManager.getHelper(getApplicationContext(),DatabaseHelper.class);
        return databaseHelper;
    }

}

FavoriteActivity(用于数据库视图)

public class FavoriteActivity extends AppCompatActivity {

    @BindView(R.id.favoritesBananaList)
    ListView bananaListView;

    List<Ingredient> ingredientList;
    Dao<Ingredient,Integer> ingredientDao;
    DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);
        ButterKnife.bind(this);

        try {
            ingredientDao = getHelper().getIntegerDao();
            ingredientList = ingredientDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        CustomAdapter adapter = new CustomAdapter(getApplicationContext(), R.layout.row_item,ingredientList);
        bananaListView.setAdapter(adapter);
    }

    private DatabaseHelper getHelper(){
        if(databaseHelper==null)
            databaseHelper = OpenHelperManager.getHelper(getApplicationContext(),DatabaseHelper.class);
        return databaseHelper;
    }
}

DatabaseHelper

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "ingredients.db";
    private static final int DATABASE_VERSION = 1;

    private Dao<Ingredient,Integer> ingredientDao;

    public DatabaseHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION, R.raw.ormlite_config);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource,Ingredient.class);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,Ingredient.class,true);
            onCreate(database,connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Dao<Ingredient,Integer> getIntegerDao() throws SQLException {
        if(ingredientDao==null)
            ingredientDao = getDao(Ingredient.class);
        return ingredientDao;
    }
}

问题是当我升级versionCode(Gradle)和databaseVersion(DatabaseHelper)时,我丢失了收藏夹中的数据。如何在升级版本后重建应用程序以保留数据?

0 个答案:

没有答案
相关问题