简体   繁体   中英

SQlite “no such table” after updating application

I updated my application today and started seeing the following in crashlitics:

android.database.sqlite.SQLiteException: no such table: LESSONATTACHMENT (code 1 SQLITE_ERROR[1]): , while compiling: SELECT _THUMB_PATH FROM LESSONATTACHMENT WHERE _id = 2

I create a new table inside my SQLiteOpenHelper , it was not in the application before I update.

This is how I created the table:

public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
} 

Should I instead place it in OnUpgrade ? Like this:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}

You Need to increase your Database Version and create the new Table in onUpgrade and Maybe delete the old Table.

Your Database is created only once. Upgrades are done in the onUpgrade function

You can also delete the Database and it will be created new with the new Table, but it depends if you are able to do this at the Moment.

for example this Code will upgrade your Database up to the current Version

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    switch(oldVersion){
        case 1:
            //Changes between version 1 and 2
        case 2:
            // Changes between Version 2 and 3
        case currentversion:
            //changes between last change and now
    }
}

when you update your database just put a new case with the new Version below this. This way you can maintain every Version of Database and make sure that it will be upgraded properly. Important is, that you don't use a break after each case.

this Code Shows for example one of my Databases, when I increase the Version I simply add a new case for the old Version. When you havent upgraded your Database yet, set the new Version to 1 higher than your old Version and add a case clause for the old Version.

public class database extends SQLiteOpenHelper {

private static String DATABASE = "Database";
private static int VERSION = 4;


public database(@Nullable Context context) {
    super(context, DATABASE, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
   db.execSQL("CREATE DATABASE TABLE");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    switch(oldVersion){
        case 1:
            db.execSQL("CHANGES DONE AFTER VERSION 1");
        case 2:
            db.execSQL("CHANGES DONE AFTER VERSION 2");
        case 3:
            db.execSQL("CHANGES DONE AFTER VERSION 3");
    }
}

Whenever you want to make changes in tables or create a table on app update, you have to change the version of database. In your case let us suppose you have upgraded from dbversion from 2 to 3 and you want to create a table for versions greater than 2.Then you have to create a table in onUpgrade like below:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(newVersion>=3){
        db.execSQL("CREATE TABLE IF NOT EXISTS `TABLE_NAME` ....");
    }
}

here I am creating the table for all the versions greater than 2 since there is a possibility that dbversion gets upgraded from 2 to 4 or some other version greater than 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM