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 7692833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:56:39+00:00 2026-05-31T20:56:39+00:00

In Android, android.database.sqlite.SQLiteStatement allows me to use prepared statements in SQLite to avoid injection

  • 0

In Android, android.database.sqlite.SQLiteStatement allows me to use prepared statements in SQLite to avoid injection attacks. Its execute method is suitable for create/update/delete operations, but there does not seem to be any method for queries that returns a cursor or the like.

Now in iOS I can create prepared statements of type sqlite3_stmt* and use them for queries, so I know this is not a limitation of SQLite. How can I perform queries with prepared statements in Android?

  • 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-31T20:56:40+00:00Added an answer on May 31, 2026 at 8:56 pm

    a prepared statement allows you to do two things

    • speed up the performance since the database does not need to parse the statement each time
    • bind & escape arguments in the statement so you are save against injection attacks

    I don’t know exactly where/when Androids SQLite implementation actually uses sqlite3_prepare (afiak not sqlite3_prepare_v2 – see here) but it does use it otherwise you could not get Reached MAX size for compiled-sql statement cache errors.

    So if you want to query the database you have to rely on the implementation there is no way I know of to do it with SQLiteStatement.

    Regarding the injection safety, every database query, insert, etc method has (sometimes alternative) versions that allow you to bind arguments.

    E.g. if you want to get a Cursor out of

    SELECT * FROM table WHERE column1='value1' OR column2='value2'
    

    Cursor SQLiteDatabase#rawQuery(

    • String sql, : full SELECT statment which can include ? everywhere
    • String[] selectionArgs : list of values that replace ?, in order they appear

    )

    Cursor c1 = db.rawQuery(
        "SELECT * FROM table WHERE column1=? OR column2=?",
        new String[] {"value1", "value2"}
    );
    

    Cursor SQLiteDatabase#query (

    • String table, : table name, can include JOIN etc
    • String[] columns, : list of the columns required, null = *
    • String selection, : WHERE clause withouth WHERE can / should include ?
    • String[] selectionArgs, : list of values that replace ?, in order they appear
    • String groupBy, : GROUP BY clause w/o GROUP BY
    • String having, : HAVING clause w/o HAVING
    • String orderBy : ORDER BY clause w/o ORDER BY

    )

    Cursor c2 = db.query("table", null, 
         "column1=? OR column2=?", 
          new String[] {"value1", "value2"},
          null, null, null);
    

    Via ContentProviders – that case is slightly different since you interact with an abstract provider, not a database. There is acutally no guarantee that there is a sqlite database backing the ContentProvider. So unless you know what columns there are / how the provider works internally you should stick to what the documentation says.

    Cursor ContentResolver#query(

    • Uri uri, : an URI representing the data source (internally translated to a table)
    • String[] projection, : list of the columns required, null = *
    • String selection, : WHERE clause withouth WHERE can / should include ?
    • String[] selectionArgs, : list of values that replace ?, in order they appear
    • String sortOrder : ORDER BY clause w/o ORDER BY

    )

    Cursor c3 = getContentResolver().query(
         Uri.parse("content://provider/table"), null,
         "column=? OR column2=?", 
          new String[] {"value1", "value2"},
          null);
    

    Hint: if you want to LIMIT here you can add it to the ORDER BY clause:

    String sortOrder = "somecolumn LIMIT 5";
    

    or depending on the implementation of the ContentProvider add it as a parameter to the Uri:

    Uri.parse("content://provider/table?limit=5");
    // or better via buildUpon()
    Uri audio = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    audio.buildUpon().appendQueryParameter("limit", "5");
    

    In all cases ? will be replaced by the escaped version of what you put in the bind argument.

    ? + "hack'me" = 'hack''me'

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

Sidebar

Related Questions

hi i am create a simple SQLite database in android. i use the SQLite
I have an SQLite database in Android and I use a ContentProvider to handle
I tried to use SQLite on Android. I want to see my database file.
Hi i am developing android application, And now i have to use sqlite database
I created a SQLite database on Android device. The program can read/write to database
I'm trying to set up a simple SQLite database in Android, handling the schema
I'm writing an Android application for RC Cars. It uses a SQLite database, and
I get frequently SQLiteDoneException, 06-29 02:03:34.816: WARN/System.err(30470): android.database.sqlite.SQLiteDoneException: not an error 06-29 02:03:34.816: WARN/System.err(30470):
I am trying to convert an existing Android, SQLite database to a multi-platform, SQLite
I get this error android.database.sqlite.SQLiteException: near %: syntax error: , while compiling: SELECT _id,

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.