Shirish Herwade
Shirish Herwade

Reputation: 11701

SQLiteException near "<" while delete query

I am trying to delete a few rows from my SQLite database like this:

dbUtilsObj.delete(EngineUtiReport.TABLE_NAME, 
    EngineUtiReport.Columns.KEY_ENGINE_UTI_UNIX_TIME + DBUtils.IS_LESS_THAN,
    new String[] { String.valueOf(nDaysOldUnixTime) });

In the above code nDayOldTimeInMills = 1429963811949 and DBUtils.IS_LESS_THAN = " < ".

But I am getting this syntax error exception and I just can't figure out what I am doing wrong:

android.database.sqlite.SQLiteException: near "<": syntax error (code 1): , 
while compiling: DELETE FROM engine_utilization_report WHERE unix_time <
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)

Upvotes: 3

Views: 207

Answers (2)

Xaver Kapeller
Xaver Kapeller

Reputation: 49807

When using parameterised queries you need to specify where you want the parameters to be inserted into your query. You do this by adding a ? where the parameter is supposed to go. Now look at the query in the exception and it becomes pretty obvious what is wrong:

DELETE FROM engine_utilization_report WHERE unix_time <

Notice the unix_time < at the end? The ? is missing. The correct query should look like this:

DELETE FROM engine_utilization_report WHERE unix_time < ?

To fix the error you just need to add the ? at the end of the where clause like this:

dbUtilsObj.delete(EngineUtiReport.TABLE_NAME, 
    EngineUtiReport.Columns.KEY_ENGINE_UTI_UNIX_TIME + DBUtils.IS_LESS_THAN + "?",
    new String[] { String.valueOf(nDaysOldUnixTime) });

Upvotes: 4

Rathan Kumar
Rathan Kumar

Reputation: 2577

You can try this:

dbUtilsObj.delete(EngineUtiReport.TABLE_NAME, 
    EngineUtiReport.Columns.KEY_ENGINE_UTI_UNIX_TIME + DBUtils.IS_LESS_THAN + "?",
    new String[] { String.valueOf(nDaysOldUnixTime) });

When you are passing an argument to the delete query you need to indicate where that argument is supposed to be placed with a ?. In your case that is at the very end of the where clause.

Upvotes: 2

Related Questions