Search code examples
androidsqliteinsertcontent-values

ContentValues put error


Keep getting this error:

Error inserting android.database.sqlite.SQLiteException: near "null": syntax error (code 1): 
, while compiling: INSERT INTO exercises(null) VALUES (NULL)

I am trying to insert values into my SQLite database when its being created. I know I should be using a prepopulated database, but I only have a couple values to insert. The first ContentValues put works, but the other two don't.

The error seems to be complaining about

database.insert("exercises", null, row2);

in this file:

MySQLiteHelper

@Override
public void onCreate(SQLiteDatabase database) {
    Log.d(MainActivity.DEBUG_TAG, "Creating db: " + DATABASE_CREATE);

    database.execSQL(DATABASE_CREATE);

    ContentValues row = new ContentValues();
    row.put(COLUMN_ID, "1");
    row.put(COLUMN_NAME, "Bench press");
    row.put(COLUMN_MUSCLE, "Chest");
    row.put(COLUMN_DESC, "Compound exercise, go heavy for 5-8 reps.");
    row.put(COLUMN_IMAGE, "http://i.imgur.com/2ATluZ4.jpg");
    database.insert("exercises", null, row);

    ContentValues row2 = new ContentValues();
    row2.put(COLUMN_ID, "2");
    row2.put(COLUMN_NAME, "Incline bench press");
    row2.put(COLUMN_MUSCLE, "Chest");
    row2.put(COLUMN_DESC, "Compound exercise, use moderate weight for 8-12 reps.");
    row2.put(COLUMN_IMAGE, "http://i.imgur.com/QgyzpZH.jpg");
    database.insert("exercises", null, row2);

    ContentValues row3 = new ContentValues();
    row3.put(COLUMN_ID, "3");
    row3.put(COLUMN_NAME, "Chest flyes");
    row3.put(COLUMN_MUSCLE, "Chest");
    row3.put(COLUMN_DESC, "Isolation exercise. Go light for 12-15 reps.");
    row3.put(COLUMN_IMAGE, "http://i.imgur.com/6Xtu7Il.png");
    database.insert("exercises", null, row3);

}

Solution

  • Try it!!!

    database.insert(" exercises ", null, row2);