SillyFidget
SillyFidget

Reputation: 197

Android: SQLite no such table when inserting rows

I am trying to insert rows into a database when the database is first created.

@Override
    public void onCreate(SQLiteDatabase db) {
        // Only uses this method when the database is first created.
        //Sets up the database - telling it the table name and all
        //the column names.
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_REPS + " TEXT NOT NULL, " +
                KEY_ABS + " TEXT NOT NULL);"    
        );

        db.execSQL("INSERT INTO DATABASE_TABLE(KEY_REPS, KEY_ABS) VALUES ('25', 'Reverse Crunches' );" );
        db.execSQL("INSERT INTO DATABASE_TABLE(KEY_REPS, KEY_ABS) VALUES ('25', 'Crunches' );" );
        db.execSQL("INSERT INTO DATABASE_TABLE(KEY_REPS, KEY_ABS) VALUES ('1 minutes', 'Plank' );" );
        db.execSQL("INSERT INTO DATABASE_TABLE(KEY_REPS, KEY_ABS) VALUES ('30', 'Toe Touches' );" );
        db.execSQL("INSERT INTO DATABASE_TABLE(KEY_REPS, KEY_ABS) VALUES ('50', 'Russian Twists' );" );

    }

If I do not have the insert calls in there the database creates fine, and I can add to it, view it, etc. However, for some reason when I try putting the calls to insert the data on create I get an error "no such table".

I tried putting the insert calls into a separate method called Populate, and calling that in my main activity, but that also produces an error.

Not sure what I am doing wrong. Any help would be greatly appreciated.

Upvotes: 0

Views: 435

Answers (1)

user
user

Reputation: 87064

Instead of using the string that names your table(represented by the DATABASE_TABLE String variable) your using the string "DATABASE_TABLE" directly(which means nothing to the database as you don't have a table DATABASE_TABLE). Update your insert calls like this:

db.execSQL("INSERT INTO " + DATABASE_TABLE + "(" + KEY_REPS +", " + KEY_ABS + ") VALUES ('25', 'Reverse Crunches' );" );

and the same for the other insert call as well(look how you declared the creation sql String). After you make this modifications, uninstall your app and reinstall it again so the changes are seen by the SQLiteOpenHelper.

Upvotes: 1

Related Questions