vendredi 4 mars 2016

SQLite no such table error when using 2 tables

I'm doing an Android project where I use a database with more than 1 table. I first tried with one table and everything worked fine, but when I'm using a second table I have an error.
Here is the code:
This part works perfectly fine:

public class MarqueDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "reprise.db";
public static final int DATABASE_VERSION = 3;

private static final String SQL_CREATE_ENTRIES = "create table marque (name text primary key)";
private static final String SQL_DELETE_ENTRIES = "drop table if exists marque";

public MarqueDBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

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

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

This one doesn't:

public class ModeleDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "reprise.db";
public static final int DATABASE_VERSION = 3;

private static final String SQL_CREATE_ENTRIES = "create table modele (name text primary key)";
private static final String SQL_DELETE_ENTRIES = "drop table if exists modele";

public ModeleDBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

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

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

The error occurs when I do an insert here:

public class ModeleDAO {
private Context context;
private ModeleDBHelper modeleHelper;
private SQLiteDatabase db;

public ModeleDAO(Context context){
    this.context=context;
}

public void addMarque(ArrayList<Modele> marqueListe){
    modeleHelper = new ModeleDBHelper(context);
    db = modeleHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    for(Modele str : marqueListe){
        values.put("name", str.getNom());
        db.insertWithOnConflict("modele", null, values, SQLiteDatabase.CONFLICT_IGNORE);

    }

    db.close();
    modeleHelper.close();
}
}

Here is the error:

android.database.sqlite.SQLiteException: no such table: modele (code 1): , while compiling: INSERT OR IGNORE  INTO modele(name) VALUES (?)
                                                                                at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                at quentin.android.fastback2.DataAccess.ModeleDAO.addMarque(ModeleDAO.java:31)
                                                                                at quentin.android.fastback2.DataAccess.SyncAdapter$3.onResponse(SyncAdapter.java:143)
                                                                                at quentin.android.fastback2.DataAccess.SyncAdapter$3.onResponse(SyncAdapter.java:125)
                                                                                at quentin.android.fastback2.Model.CustomRequest.deliverResponse(CustomRequest.java:42)
                                                                                at quentin.android.fastback2.Model.CustomRequest.deliverResponse(CustomRequest.java:16)
                                                                                at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I've tried a couple of things, like changing the database version, uninstall/reinstall the app and using an other device emulator (I'm using Genymotion)
I checked the database file, the table "marque" is well created but not the table "modele". It's like the OnCreate() method from ModeleDBHelper is never called. Thanks for your help

Aucun commentaire:

Enregistrer un commentaire