0

I'm using Ormlite, for my android app, to store my data. I want to download a database from a site and after this, the program checks if a database exists. If a database exists, then the new database and the old database should merge. Here comes my problem. In Ormlite you create a Dao to interact with the tables of a database and I wanted to know, how I can interact with the new database after the download?

For example: Database A from the app has the items:

Cheese, Eggs, Milk, Chicken

Database B from the download has the items:

Cheese, Eggs, Milk, Flour, Bread

At the end I want:

Cheese, Eggs, Milk, Flour, Bread, Chicken and only one database left.

And to clarify: Yes, it exists questions "How to merge databases".
For example: Android update - merge database contents?
But that is not my problem. I want to know, how to interact with the new database with Ormlite. Which way exists, that I can get access to the new database?

Edit:
I tried to download the database and guarantee that the version is always higher than the old database version. Therefore, at least that what I thought, it would go in OnUpgrade method and there I could merge the database. But i just replaced the old database.
I also tried to make a second OrmDBHelper with another name, so after the download I could use the methods there. But no errors or anything whatsoever.
Here is the database helper from the existing database and the second one is the same, except the name.

public class OrmDbHelper extends OrmLiteSqliteOpenHelper {

public static final String LOG = OrmDbHelper.class.getName();
public static final String DB_NAME = "bestand.db";
public static final int DB_VERSION = 1;

public OrmDbHelper(Context context) {
    super(context, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
            + File.separator + DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    try {
        TableUtils.createTableIfNotExists(connectionSource, ScanItem.class);
        TableUtils.createTableIfNotExists(connectionSource, BestandItem.class);
        TableUtils.createTableIfNotExists(connectionSource, SettingsItem.class);
    } catch (SQLException ex) {
        Log.e(LOG, "Error Creating table", ex);
    }
}

@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    try {
        TableUtils.dropTable(connectionSource, ScanItem.class, true);
        TableUtils.dropTable(connectionSource, BestandItem.class, true);
        TableUtils.dropTable(connectionSource, SettingsItem.class, true);
        TableUtils.createTable(connectionSource, ScanItem.class);
        TableUtils.createTable(connectionSource, BestandItem.class);
        TableUtils.createTable(connectionSource, SettingsItem.class);
    } catch (SQLException ex) {
        Log.e(LOG, "Error Updating table", ex);
    }
}

public Dao<ScanItem, Integer> createScanItemDAO() {
    try {
        return DaoManager.createDao(connectionSource, ScanItem.class);
    } catch (SQLException ex) {
        Log.e(LOG, "Error Creating DAO for Todo class", ex);
    }
    return null;
}

public Dao<BestandItem, Integer> createBestandItemDAO() {
    try {
        return DaoManager.createDao(connectionSource, BestandItem.class);
    } catch (SQLException ex) {
        Log.e(LOG, "Error Creating DAO for Todo class", ex);
    }
    return null;
}

public Dao<SettingsItem, Integer> createSettingItemDAO() {
    try {
        return DaoManager.createDao(connectionSource, SettingsItem.class);
    } catch (SQLException ex) {
        Log.e(LOG, "Error Creating DAO for Todo class", ex);
    }
    return null;
}
}

And here is how I tried to get access to the second database: The access to the first database works, but to the second one doesnt. I even checked if the download of the second database works, which it does.

public class OrmDataHelper {

private Dao<ScanItem, Integer> scanItemDAO;
private Dao<BestandItem, Integer> bestandItemDAO;
private Dao<SettingsItem, Integer> settingsItemsDAO;

public OrmDataHelper(Context context) {
    OrmDbHelper ormDbHelper = new OrmDbHelper(context);
     scanItemDAO = ormDbHelper.createScanItemDAO();
     bestandItemDAO = ormDbHelper.createBestandItemDAO();
     settingsItemsDAO = ormDbHelper.createSettingItemDAO();
}

public ArrayList<ScanItem> getAllScanItem() {
    ArrayList<ScanItem> temp = null;
    try {
         temp = new ArrayList<>(scanItemDAO.queryForAll());
    } catch (SQLException ex) {
        ex.getMessage();
    }
    return temp;
}
Christopher
  • 59
  • 10
  • what have you tried so far – dangee1705 May 17 '18 at 15:43
  • I tried to download the database and guarantee that the version is always higher than the old database version. Therefore, at least that what I thought, it would go in OnUpgrade method and there I could merge the database. But i just replaced the old database. – Christopher May 17 '18 at 16:19
  • What SQL commands have you tried? Can you show some code? – Gray May 19 '18 at 14:43
  • Sure, just added it to the question. This is the code from my second attempt. – Christopher May 19 '18 at 16:48

0 Answers0