SQLiteException near "<" when deleting a query

I am trying to delete multiple rows from a 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 cannot 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)

      

+3


source share


2 answers


When using parameterized queries, you need to specify where you want the parameters to be inserted into your query. You do this by adding ?

where the parameter should go. Now look at the request in the exception and it becomes pretty obvious what is wrong:

DELETE FROM engine_utilization_report WHERE unix_time <

      

Pay attention to unix_time <

the end? ?

is absent. The correct query should look like this:



DELETE FROM engine_utilization_report WHERE unix_time < ?

      

To fix the error, you just need to add ?

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) });

      

+4


source


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 pass an argument to a delete request, you need to specify where that argument should be placed with ?

. In your case, this is at the very end of the where clause.

+2


source







All Articles