How can I handle this sqlite error?

1

I have a class% co_of% parent:

public class DB extends SQLiteOpenHelper {

private static final int VERSION_BASEDATOS = 1;
private static final String NOMBRE_BASEDATOS = "DB.db";
private Context context;

public DataExpoAuto(Context context) {
    super(context, NOMBRE_BASEDATOS, null, VERSION_BASEDATOS);
    this.context = context;

}

@Override
public void onCreate(SQLiteDatabase 
    db.execSQL(TFavorito.create);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
    db.execSQL("DROP TABLE IF EXISTS " + TFavorito.TAG);
    onCreate(db);
}

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

public Context getContext() {
    return context;
}

public void setContext(Context context) {
    this.context = context;
}

Then to create separate tables I do inheritance:

public class TFavorito  extends DB{

public static final String TAG = TFavorito.class.getSimpleName().toLowerCase();

public static final String[] array={"id","titulo","descripcion","tipo"};

public static final String create= "CREATE TABLE "+TAG+" ( "+array[0]+" INTEGER, "
        +array[1]+" TEXT NOT NULL, "+array[2]+" TEXT NOT NULL, "+array[3]+" INTEGER );";


private static TFavorito INSTANCE = null;

// creador sincronizado para protegerse de posibles problemas  multi-hilo
// otra prueba para evitar instanciación múltiple
private synchronized static void createInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE = new TFavorito(context);
    }
}

public static TFavorito getInstance(Context context) {
    if (INSTANCE == null) createInstance(context);
    return INSTANCE;
}

public TFavorito(Context context) {
    super(context);
}

public void insertar(Favorito favorito){

    SQLiteDatabase db = getWritableDatabase();
    if (db != null) {
        db.insert(TAG, null, favorito.getContentValues(array));
        Log.d(TAG, "Inserto correctamente "+TAG);
    }
    db.close();

}

public Favorito leer(int id,int tipo) {
    Favorito favorito = null;

    String selection =  array[0]+" = ? AND "+array[3]+" = ?";
    // Specify arguments in placeholder order.
    String[] selectionArgs = {String.valueOf(id),String.valueOf(tipo)};

    SQLiteDatabase db = getReadableDatabase();
    Cursor c = db.query(TAG, array, selection, selectionArgs, null, null, null, null);

    if((c != null)&& c.moveToFirst()) {
        favorito = new Favorito();
        favorito.parceObaject(c);
        Log.d(TAG, "Recupero correctamente "+TAG);
        c.close();
    }
    db.close();
    return favorito;
}

Insert the data well and recover well, but when I consult data that is not in the table the error comes out:

FATAL EXCEPTION: main
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developerdj.expoauto/com.developerdj.expoauto.vista.activity.ConcesionariaActiviy}: android.database.sqlite.SQLiteException: no such table: tfavorito (code 1): , while compiling: SELECT id, titulo, descripcion, tipo FROM tfavorito WHERE id = ? AND tipo = ?
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: android.database.sqlite.SQLiteException: no such table: tfavorito (code 1): , while compiling: SELECT id, titulo, descripcion, tipo FROM tfavorito WHERE id = ? AND tipo = ?
                                                 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
                                                 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
                                                 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                 at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                 at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                 at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
                                                 at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
                                                 at com.developerdj.expoauto.data.TFavorito.leer(TFavorito.java:65)
    
asked by Diego 27.06.2017 в 22:26
source

2 answers

0

The error you mention is due to the existence of a version of the database with created table structures which can not be modified. In this case the database was created but without the table you need.

  

Caused by: android.database.sqlite.SQLiteException: not such table:   tfavorito (code 1):, while compiling: SELECT id, title, description,   type FROM tfavorito WHERE id =? AND type =?

As an option it is to delete the data from the application or simply delete it and upload it again so that the new database with the required tables is created.

But remember that in production, you may need to modify the structure of a table and it is not an option for users to delete the application or its data.

For this, it is precisely to change the version of the database and to call again the onUpgrade() method that precisely makes a Drop of the table and the creation of the new structure:

public class DB extends SQLiteOpenHelper {

private static final int VERSION_BASEDATOS = 2; //***
private static final String NOMBRE_BASEDATOS = "DB.db";
private Context context;

public DataExpoAuto(Context context) {
    super(context, NOMBRE_BASEDATOS, null, VERSION_BASEDATOS);
    this.context = context;

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
    db.execSQL("DROP TABLE IF EXISTS " + TFavorito.TAG);
    onCreate(db);
}
    
answered by 29.06.2017 в 01:06
0

suppose that your database is not created because execSQL does not return a value. The error says 'no such table'. On your insert method check the value returned by 'insert' statement, check it's not it's not -1.

    
answered by 29.06.2017 в 11:41