Error with sqlite database on android

2

I have this code to manage an sqlite database on android:

public class ConfiguracionSQLite extends SQLiteOpenHelper {

    public ConfiguracionSQLite(Context context) {
        super(context, "configuracion", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE configuracion (id_configuracion INTEGER, contenido TEXT)");
    }

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

    }

    public void inicializarTabla() {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("INSERT INTO configuracion (id_configuracion) VALUES(1)");
        db.close();
    }

    public Vector<String> comprobarTabla() {
            Vector<String> result = new Vector<>();
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.rawQuery("SELECT * FROM configuracion", null);
            while (cursor.moveToNext()) {
                result.add(cursor.getString(0));//id_configuracion
                result.add(cursor.getString(1));//contenido
            }
            cursor.close();
            db.close();
            return result;
    }
}

So far so good. The problem comes when I add one more field to the table:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE configuracion (id_configuracion INTEGER, contenido TEXT, contenido_2 TEXT)");
}

public Vector<String> comprobarTabla() {
        Vector<String> result = new Vector<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM configuracion", null);
        while (cursor.moveToNext()) {
            result.add(cursor.getString(0));//id_configuracion
            result.add(cursor.getString(1));//contenido
            result.add(cursor.getString(2));//contenido_2
        }
        cursor.close();
        db.close();
        return result;
}

If I uninstall the application and reinstall it, there is no problem, but if what I do is an update, then I have an error here: result.add(cursor.getString(2));//contenido_2

How can I update the sqlite database when I update?

This is the error log:

  

java.lang.IllegalStateException: Could not read row 1, col 2 from   CursorWindow. Make sure the Cursor is initialized correctly before   accessing data from it.

    
asked by Alberto Mier 22.11.2017 в 11:07
source

1 answer

2

Each time you change any table in your database, you need to increase the number of database versions in your AndroidManifest or build.gradle depending on how it is configured.

Do not forget to also modify the alteration of your table in your Sqlite oCreate method for those who install your app for the first time.

After that, for those who already had your app installed before the data alteration, the application will enter the following method:

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

         }

There you must write your script to alter your table (add new campp, etc)

For more info: link

    
answered by 22.11.2017 / 12:25
source