I have an android app with a sqllite database. This database has various tables. So far I have two tables and I am having an error: when I create the table A first, when I run the code for the table B my app crashes; when I create the table B first and then the A, the app also crashes. I have to clear my app cache so I can "change" the acess, so I'm never able to access both in the same app "run". I think its because Im recreating the database when I add the new table. To avoid that, I tried to check if the database already exists and, if does, not recreate but I'm checking it in the constructor and android doesn't allow it.
log:
12-25 00:30:28.024 32031-32031/com.support.android.iplfit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.support.android.iplfit, PID: 32031 android.database.sqlite.SQLiteException: no such table: Dica (code 1): , while compiling: DELETE FROM Dica 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.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1499) at com.support.android.iplfit.BDHelpers.DicaBDHelper.removerAllDicasBD(DicaBDHelper.java:73) at com.support.android.iplfit.Singletons.SingletonDicas.adicionarDicasBD(SingletonDicas.java:65) at com.support.android.iplfit.Singletons.SingletonDicas$1.onResponse(SingletonDicas.java:83) at com.support.android.iplfit.Singletons.SingletonDicas$1.onResponse(SingletonDicas.java:77) at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
And one of the classes where Im creating one table:
public class DicaBDHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "db_iplfit";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "Dica";
private static final String ID_DICA = "id";
private static final String CHANNEL_DICA = "channel";
private static final String TITULO_DICA = "titulo";
private static final String CONTEUDO_DICA = "conteudo";
private final SQLiteDatabase database;
public DicaBDHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.database = getWritableDatabase();
}
private static boolean haveDB(Context context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
@Override
public void onCreate(SQLiteDatabase database) {
String createDicaTable = "CREATE TABLE " + TABLE_NAME + "( " +
ID_DICA + " INTEGER UNSIGNED PRIMARY KEY," +
CHANNEL_DICA + " TEXT NOT NULL," +
TITULO_DICA + " TEXT NOT NULL," +
CONTEUDO_DICA + " TEXT NOT NULL" + ")";
database.execSQL(createDicaTable);
}
@Override
public void onUpgrade(SQLiteDatabase database, int i, int j) {
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
database.execSQL(sql);
//this.onCreate(database);
}
}
haveDB()?onCreate()is called. (You can optionally populate the tables with initial/default values at that time.) An activity will interact with your entire database, not just one table, so the order of activities should never make a difference in how the db is created, just the order in which the tables are used.