Search code examples
androiddatabasesqlitemultiple-tablessqliteopenhelper

Second Table in SQLite Database is not being created


I know there have been numerous questions asked on this topic before, but none of those questions/answers helped. I am creating an android app and I have a database with multiple tables. The first one created successfully, but the second one never created. It gives me a "table does not exist" error when I try to access the table later. I have tried numerous times changing the database name, changing the database version, clearing the cache and data, uninstalling and reinstalling my app. Nothing works. I used sqlite browser to look at my database and the second table wasn't there. I have tried putting breakpoints in the onCreate method of my SQLiteDatabaseOpenHelper after increasing the version. The debugger passed right through. Here is the applicable code: First Table DBHelper:

public class SessionDescriptionsDatabaseOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static final String TABLE_SESSIONDESCRIPTIONS = "session_descriptions";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DESCRIPTION = "description";

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

@Override
public void onCreate(SQLiteDatabase db) {


    // Database creation SQL statement
    final String DATABASE_CREATE = "create table "
            + TABLE_SESSIONDESCRIPTIONS
            + "("
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_TITLE + " text not null, "
            + COLUMN_DESCRIPTION + " text not null"
            + ");";
    db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONDESCRIPTIONS);

    // Create tables again
    onCreate(db);
}//more non-applicable code goes here
}

Here is my Second Table DBHelper:

public class SessionLeadersDatabaseOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static final String TABLE_SESSIONLEADERS = "session_leaders";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "category";
private static final String COLUMN_ORGANIZATION = "summary";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_PICURL = "google";
public SessionLeadersDatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {


    // Database creation SQL statement
    final String DATABASE_CREATE = "create table "
            + TABLE_SESSIONLEADERS
            + "("
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_NAME + " text not null, "
            + COLUMN_ORGANIZATION + " text not null,"
            + COLUMN_TITLE + " text not null,"
            + COLUMN_DESCRIPTION + " text not null, "
            + COLUMN_PICURL + " text not null"
            + ");";
    db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONLEADERS);

    // Create tables again
    onCreate(db);
}//more non-applicable code goes here
}

This is my call for the first table(in background thread in AsyncTask):

SessionLeadersDatabaseOpenHelper sldb = new SessionLeadersDatabaseOpenHelper(applicationContext);

and the second(in background thread in AsyncTask):

SessionDescriptionsDatabaseOpenHelper sddb = new SessionDescriptionsDatabaseOpenHelper(applicationContext);

The only way I could get it to work was to have a separate database for each table, but that is very, very poor design and take up unneeded space on the device. I executed the sql statement for creating the second table in sqlite browser and it worked fine. I have searched many tutorials and stackoverflow questions and none of them helped. I have been stuck on this for three days. Help is greatly appreciated. Thanks in advance.


Solution

  • Create both the Tables in one single helper class

    Try this =

        public class SessionDescriptionsDatabaseOpenHelper extends SQLiteOpenHelper {
    
        // Fields for first table
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "database.db";
        private static final String TABLE_SESSIONDESCRIPTIONS = "session_descriptions";
        private static final String COLUMN_ID = "_id";
        private static final String COLUMN_TITLE = "title";
        private static final String COLUMN_DESCRIPTION = "description";
    
        // Fields for second table
        private static final String TABLE_SESSIONLEADERS = "session_leaders";
        private static final String COLUMN_NAME = "category";
        private static final String COLUMN_ORGANIZATION = "summary";
        private static final String COLUMN_PICURL = "google";
    
    
    
    
    
        public SessionDescriptionsDatabaseOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
    
      public static final String DATABASE_CREATE_TABLE1 = "create table "
                        + TABLE_SESSIONDESCRIPTIONS
                        + "("
                        + COLUMN_ID + " integer primary key autoincrement, "
                        + COLUMN_TITLE + " text not null, "
                        + COLUMN_DESCRIPTION + " text not null"
                        + ");";
    
           public static final String DATABASE_CREATE_TABLE2 = "create table "
                    + TABLE_SESSIONLEADERS
                    + "("
                    + COLUMN_ID + " integer primary key autoincrement, "
                    + COLUMN_NAME + " text not null, "
                    + COLUMN_ORGANIZATION + " text not null,"
                    + COLUMN_TITLE + " text not null,"
                    + COLUMN_DESCRIPTION + " text not null, "
                    + COLUMN_PICURL + " text not null"
                    + ");";
    
        @Override
        public void onCreate(SQLiteDatabase db) {
    
    
            // Database creation SQL statement
    
            db.execSQL(DATABASE_CREATE_TABLE1);
            db.execSQL(DATABASE_CREATE_TABLE2);
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONDESCRIPTIONS);
    
            // Create tables again
            onCreate(db);
        }//more non-applicable code goes here
        }
    

    EDIT : I HAVE EDITED THE CODE