-1

I read all other "SQLiteException no such table" messages but didn't find an answer to my point.

I'm creating an android application with multiple tables in database. I build it step by step. I created first table A, and a form to add/edit data. I inspired myself with LoaderThrottle android sample. It was working perfectly. After this, I created table B, exactly like table A, and a form to add data.

I then saw I forgot a field in table B. I tried to add this field and switched table B to version 2. But I had a lot of problem. "downgrade" and "upgrade" errors... So I erased application data and un-installed the application from device, so database is deleted and recreated on first launch.

But now, when I launch the application, I have the exception << android.database.sqlite.SQLiteException: no such table: A: , while compiling: SELECT name, _id FROM A WHERE ((active = 1 ))>> ?!?

Why is dbHelper not creating the database? Do I have to explicitly call its onCreate method? Do I need unique dbHelper for all tables of database or one dbHelper for each table (what I have now)?

Further information : - In manifest, I have two providers, linked to content provider of each table.

Edit : Here is creation code for each table.

Table A (for which I have the exception) :

       public void onCreate(SQLiteDatabase db) {
       db.execSQL("CREATE TABLE " + ActivityTable.TABLE_NAME + " ("
               + ActivityTable._ID                + " INTEGER PRIMARY KEY, "
               + ActivityTable.COLUMN_NAME_TITLE  + " TEXT NOT NULL, "
               + ActivityTable.COLUMN_NAME_CODE   + " TEXT, "
               + ActivityTable.COLUMN_NAME_ACTIVE + " smallint NOT NULL  DEFAULT 1, "
               + ActivityTable.COLUMN_NAME_CPTI   + " smallint NOT NULL  DEFAULT 1 "
               + ");");
   }

Table B :

       public void onCreate(SQLiteDatabase db) {
       db.execSQL("CREATE TABLE " + TrackTable.TABLE_NAME + " ("
               + TrackTable._ID                     + " INTEGER PRIMARY KEY, "
               + TrackTable.COLUMN_NAME_DATE        + " long NOT NULL, "
               + TrackTable.COLUMN_NAME_ACTIVITY_ID + " int NOT NULL DEFAULT 0, " 
               + TrackTable.COLUMN_NAME_DAYPART     + " smallint NOT NULL  DEFAULT 0, "
               + TrackTable.COLUMN_NAME_NPQ         + " smallint NOT NULL  DEFAULT 0, " 
               + TrackTable.COLUMN_NAME_NBPID       + " long NOT NULL  DEFAULT 0 " 
               + ");");
   }

Thanks in advance for any clue. Florent

zFlorent
  • 21
  • 4

1 Answers1

1

After multiple tries, I finally got myself unstuck.

I give you my solution, even if I don't really understand why I have to do so. Since my question had no answer, it could help someone someday.

As a reminder, I started from LoaderThrottle android sample. Please refer to this file.

In method onCreate of my child class derived from SQLiteOpenHelper, I change :

db.execSQL("CREATE TABLE " + ...

by :

db.execSQL("CREATE TABLE IF NOT EXISTS " + ...

Then, in onCreate method of child class derived from ContentProvider, I directly call DbHelper onCreate method :

     /**
     * Perform provider creation.
     */
    @Override
    public boolean onCreate() {
        mOpenHelper = new ActivityDbHelper(getContext());
        // Assumes that any failures will be reported by a thrown exception.

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        mOpenHelper.onCreate(db);

        return true;
    }

Enjoy, Florent

zFlorent
  • 21
  • 4
  • I don't know why people downvoted your question, but it was exactly my problem, and my solution. thanks a lot! – gezgingun Jan 24 '16 at 10:47