Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:07:31+00:00 2026-05-30T08:07:31+00:00

I am getting the following error when I am using the code below. 02-24

  • 0

I am getting the following error when I am using the code below.

02-24 12:13:16.972: E/AndroidRuntime(11024): Caused by: android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: DELETE FROM messages WHERE ? NOT IN ( SELECT ? FROM ? WHERE ?=? ORDER BY ? DESC LIMIT ?) AND ?=?
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
02-24 12:13:16.972: E/AndroidRuntime(11024):    at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1623)

Code:

// query idea from
// http://stackoverflow.com/questions/6528117/keep-only-n-last-records-in-sqlite-database-sorted-by-date
// I have added the AND ?=? because I only want to delete records from a particular conversation thread, not the entire table.
String whereClause = "? NOT IN ( SELECT ? FROM ? WHERE ?=? ORDER BY ? DESC LIMIT ?) AND ?=?";

String[] whereArgs = new String[]{
        DbAdapter.COL_ROWID,
        DbAdapter.COL_ROWID,
        DBConstants.DB_TABLE_MESSAGES,
        DbAdapter.COL_CONVERSATION_ID,
        String.valueOf(conversationId),
        DbAdapter.COL_TIMESTAMP,
        String.valueOf(numMessagesToRetain),
        DbAdapter.COL_CONVERSATION_ID,
        String.valueOf(conversationId)
};

deleted = mDb.delete(DBConstants.DB_TABLE_MESSAGES, whereClause,
        whereArgs);

Also, the Android documentation for the SQLite delete method seems to be incomplete regarding the whereArgs parameter.

public int delete (String table, String whereClause, String[] whereArgs)

Since: API Level 1
Convenience method for deleting rows in the database.
Parameters

table   the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
Returns

the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.

It works fine if I use the following where clause:

String whereClause = String
.format("%s NOT IN ( SELECT %s FROM %s WHERE %s=%s ORDER BY %s DESC LIMIT %s) AND %s=%s",
        DbAdapter.COL_ROWID,
        DbAdapter.COL_ROWID,
        DBConstants.DB_TABLE_MESSAGES,
        DbAdapter.COL_CONVERSATION_ID,
        String.valueOf(conversationId),
        DbAdapter.COL_TIMESTAMP,
        String.valueOf(numMessagesToRetain),
        DbAdapter.COL_CONVERSATION_ID,
        String.valueOf(conversationId));

The only thing I can think of is that the delete method does not support parameterised queries, but the following query works, so it rules out that.

mDb.delete(DB_TABLE_MESSAGES, COL_ROWID + "=?",
                new String[]{ String.valueOf(rowId)});

Solution

Thanks to antlersoft, the solution is like so:

String whereClause = String
        .format("(%s NOT IN ( SELECT %s FROM %s WHERE %s=? ORDER BY %s DESC LIMIT ?)) AND %s=?",
                DbAdapter.COL_ROWID,
                DbAdapter.COL_ROWID,
                DBConstants.DB_TABLE_MESSAGES,
                DbAdapter.COL_CONVERSATION_ID,
                DbAdapter.COL_TIMESTAMP,
                DbAdapter.COL_CONVERSATION_ID);

String[] whereArgs = new String[]{
        String.valueOf(conversationId),
        String.valueOf(numMessagesToRetain),
        String.valueOf(conversationId)
};

deleted = mDb.delete(DBConstants.DB_TABLE_MESSAGES, whereClause,
        whereArgs);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:07:33+00:00Added an answer on May 30, 2026 at 8:07 am

    AFAIK, you are trying to use parameterization where it is not allowed.

    You can only use parameterization to replace what would otherwise be constant values in your query.

    You are trying to use it to replace identifiers, as well, which won’t work.

    If you want the identifiers to be dynamic, replace them via string manipulation before you pass the query to SQLite; don’t pass them as ? and expect SQLite to make sense of the query..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error when using the code below the error. can
I am using the Android code below to connect to Facebook but getting the
Am getting the same error on the code below using either trusted or SQL
I am getting following error message when using Doctrine ORM in Codeigniter. ( !
I'm getting the following error in Eclipse using the Cusp plug-in: Package LISP-UNIT is
I am getting following error when I am trying to get WebResponse using WebResponse
I am getting the following error: Access denied for user 'apache'@'localhost' (using password: NO)
I'm getting the following error when trying to build my app using Team Foundation
I am getting the following error when trying to update an object using nhibernate.
I'm getting the following error when trying to run a JSP. I'm using Tomcat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.