3

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)
0

2 Answers 2

4

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) });
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.