Search code examples
androidandroid-sqliteandroid-sql

SQLiteException: near "occured": syntax error (code 1)


I am trying to insert a row into a table in my database but I am stuck. The insert function is returning -1 and the Logcat is showing the following error:

05-06 20:58:45.446 13631-13631/com.example.android.disasterreporter E/SQLiteLog: (1) near "occured": syntax error

05-06 20:58:45.447 13631-13631/com.example.android.disasterreporter E/SQLiteDatabase: Error inserting disaster=3 last occured=5 location=loc predictedtime=2 prediction votes=1 votes for past=7 _id=2 predictedseverity=1 past severity=1

android.database.sqlite.SQLiteException: near "occured": syntax error (code 1): , while compiling: INSERT INTO disasters(disaster,last occured,location,predictedtime,prediction votes,votes for past,_id,predictedseverity,past severity) VALUES (?,?,?,?,?,?,?,?,?)

This is the contract class:

public final class DisasterContract {

private DisasterContract() {}

public static final class DisasterEntry implements BaseColumns {

    public final static String TABLE_NAME = "disasters";

    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_DISASTER = "disaster";
    public final static String COLUMN_PAST_SEVERITY = "past severity";
    public final static String COLUMN_LOCATION = "location";
    public final static String COLUMN_LAST_OCCURED = "last occured";
    public final static String COLUMN_VOTES_FOR_PAST = "votes for past";
    public final static String COLUMN_PREDICTION_TIME = "predictedtime";
    public final static String COLUMN_PREDICTION_SEVERITY = "predictedseverity";
    public final static String COLUMN_PREDICTION_VOTES = "prediction votes";

    public static final int LOW = 0;
    public static final int MEDIUM = 1;
    public static final int HIGH = 2;
    public static final int EARTHQUAKE = 0;
    public static final int FLOOD = 1;
    public static final int LANDSLIDE = 2;
    public static final int TERROR_ATTACK = 3;

}}

This is my helper class:

public class DisasterDBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "disasterreporter.db";
private static final int DATABASE_VERSION = 1;

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

}

@Override
public void onCreate(SQLiteDatabase db) {
    String SQL_CREATE_TABLE = "CREATE TABLE " + DisasterEntry.TABLE_NAME
            + " ( " + DisasterEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DisasterEntry.COLUMN_DISASTER + " INTEGER NOT NULL, "
            + DisasterEntry.COLUMN_LOCATION + " TEXT NOT NULL, "
            + DisasterEntry.COLUMN_LAST_OCCURED + " INTEGER NOT NULL, "
            + DisasterEntry.COLUMN_PAST_SEVERITY + " INTEGER NOT NULL DEFAULT 1, "
            + DisasterEntry.COLUMN_VOTES_FOR_PAST + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_SEVERITY
            + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_TIME
            + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_VOTES
            + " INTEGER NOT NULL DEFAULT 1 );";
    db.execSQL(SQL_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

This is the SQL statement that is showing the error:

ContentValues values = new ContentValues();

                                        values.put(DisasterContract.DisasterEntry._ID,2);
                values.put(DisasterContract.DisasterEntry.COLUMN_DISASTER, DisasterContract.DisasterEntry.TERROR_ATTACK);
    values.put(DisasterContract.DisasterEntry.COLUMN_LOCATION,"loc");
    values.put(DisasterContract.DisasterEntry.COLUMN_LAST_OCCURED, 5);
    values.put(DisasterContract.DisasterEntry.COLUMN_PAST_SEVERITY, DisasterContract.DisasterEntry.MEDIUM);
    values.put(DisasterContract.DisasterEntry.COLUMN_VOTES_FOR_PAST, 7);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_SEVERITY,1);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_TIME,2);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_VOTES,1);
          long newRowId = db.insert(DisasterContract.DisasterEntry.TABLE_NAME, null, values);
          Log.d("Row ID",""+newRowId);

Solution

  • SQL column names should not have spaces in them. Change COLUMN_LAST_OCCURED from last occured to last_occured and see if it works for you.

    Same for COLUMN_VOTES_FOR_PAST, COLUMN_PAST_SEVERITY and COLUMN_PREDICTION_VOTES.