0

I already have a database in my app using a 3rd party library. The library doesn't seem to have drop table functionality. So I was thinking to directly change it using SQLiteOpenHelper API. Is this possible?

I have created a class that extends SQLiteOpenHelper and give it the same db name as the one used in the library.

public class SqliteDatabaseHelper extends SQLiteOpenHelper {

    // Database Info
    private static final String DATABASE_NAME = "myDatabase";
    private static final int DATABASE_VERSION = 1;

    private Context context;

    public SqliteDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        L.w("old: " + oldVersion + ", " + "new: " + newVersion);
        if (oldVersion != newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + "deals_latest");
            onCreate(db);
            L.w("drop it like it's hot");
        }
    }
}

And I initialize it in Application class, just to see if it reflects the db class I created.

SQLiteDatabase db = new SqliteDatabaseHelper(this).getReadableDatabase();
L.w("version: " + db.getVersion());
L.w("path: " + db.getPath());

SqliteDatabaseHelper helper = new SqliteDatabaseHelper(this);
helper.onUpgrade(db, 1, 2); // this line suppose to invoke the drop table query

When running the app, onUpgrade() method doesn't seem to be called.

Mind you, I have never had any experience in using the native SQLite helper, so I have no idea what is going on here. My objective is just to see if the query in onUpgrade is executed or not. But the table still exists in the database.

How do I get around this?

4
  • If you change the constant DATABASE_VERSION in your code to an integer above one, the onUpgrade() will get called. As for your original question I still need some further information. It will be possible for both your SQLiteHelper class and library to access the same database but you'll have to be careful of livelocks and deadlocks and data integrity. Commented Jul 28, 2016 at 8:35
  • I see. Thanks for the info. If that's the case, I do not need to call onUpgrade explicitly, just change the DATABASE_VERSION, am I right? Commented Jul 28, 2016 at 8:43
  • Yes. Next time you instantiate you SQLiteOpenHelper this call super(context, DATABASE_NAME, null, DATABASE_VERSION); checks to see if the database on device storage matches the one the running application requires. Commented Jul 28, 2016 at 8:48
  • Check this link for Google's documentation on SQLiteOpenHelper: developer.android.com/reference/android/database/sqlite/… Commented Jul 28, 2016 at 8:49

1 Answer 1

1

The SQLiteOpenHelper class helps to manage versions of your own database.

It does not make sense to use it for a database that is managed by third-party code.

Just open the database directly with openDatabase().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.