0

I have a 1(one) Table at first in my database. Then, I realized that I will add another table in that database.

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE if not exists profile (" +
            "email text primary key not null, " +
            "username text, " +
            "profilePicture text, " +
            "about text)";
    db.execSQL(sql);

    sql = "CREATE TABLE if not exists current_user (" +
            "email text)";
    db.execSQL(sql);

    sql = "INSERT INTO current_user(email)" +
            "VALUES ('')";
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXISTS profile";
    db.execSQL(sql);
    sql = "DROP TABLE IF EXISTS current_user";
    db.execSQL(sql);

    onCreate(db);
}

When I want to update the email of my current_user it sends me an exception

public void setCurrentEmail(String email){
    ContentValues rsValues = new ContentValues();
    rsValues.put("email", email);

    database.update("current_user", rsValues, null, null);
}

no such table: current_user (code 1):

Others say that, you must change the name of the Database for it to work but are there any alternatives?

  • If you want to preserve data, implement `onUpgrade()`. If you're fine with losing database data, uninstall + reinstall works. – laalto Aug 07 '18 at 19:30

1 Answers1

0

If you did not uninstall the app after updating the code of your SQLiteOpenHelper class, then you must.
The onCreate() method is invoked only the 1st time that the db is created. So now there is no table current_user in the db.
Uninstall the app form your device or emulator so the db is deleted and start over.