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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:26:09+00:00 2026-06-04T03:26:09+00:00

I am using the query method of SQLiteDatabase. How do I use the query

  • 0

I am using the query method of SQLiteDatabase. How do I use the query method?

I tried this:

Cursor cursor = sqLiteDatabase.query(
    tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);

tableColumns – columns parameter is constructed as follows.

String[] columns = new String[]{KEY_ID, KEY_CONTENT};

If we need to get all the fields, how should the column parameter to be constructed. Do we need to include all the Field Names in String array?

How do I properly use the query method?

  • 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-06-04T03:26:10+00:00Added an answer on June 4, 2026 at 3:26 am

    tableColumns

    • null for all columns as in SELECT * FROM ...
    • new String[] { "column1", "column2", ... } for specific columns as in SELECT column1, column2 FROM ... – you can also put complex expressions here:
      new String[] { "(SELECT max(column1) FROM table1) AS max" } would give you a column named max holding the max value of column1

    whereClause

    • the part you put after WHERE without that keyword, e.g. "column1 > 5"
    • should include ? for things that are dynamic, e.g. "column1=?" -> see whereArgs

    whereArgs

    • specify the content that fills each ? in whereClause in the order they appear

    the others

    • just like whereClause the statement after the keyword or null if you don’t use it.

    Example

    String[] tableColumns = new String[] {
        "column1",
        "(SELECT max(column1) FROM table2) AS max"
    };
    String whereClause = "column1 = ? OR column1 = ?";
    String[] whereArgs = new String[] {
        "value1",
        "value2"
    };
    String orderBy = "column1";
    Cursor c = sqLiteDatabase.query("table1", tableColumns, whereClause, whereArgs,
            null, null, orderBy);
    
    // since we have a named column we can do
    int idx = c.getColumnIndex("max");
    

    is equivalent to the following raw query

    String queryString =
        "SELECT column1, (SELECT max(column1) FROM table1) AS max FROM table1 " +
        "WHERE column1 = ? OR column1 = ? ORDER BY column1";
    sqLiteDatabase.rawQuery(queryString, whereArgs);
    

    By using the Where/Bind -Args version you get automatically escaped values and you don’t have to worry if input-data contains '.

    Unsafe: String whereClause = "column1='" + value + "'";
    Safe: String whereClause = "column1=?";

    because if value contains a ' your statement either breaks and you get exceptions or does unintended things, for example value = "XYZ'; DROP TABLE table1;--" might even drop your table since the statement would become two statements and a comment:

    SELECT * FROM table1 where column1='XYZ'; DROP TABLE table1;--'
    

    using the args version XYZ'; DROP TABLE table1;-- would be escaped to 'XYZ''; DROP TABLE table1;--' and would only be treated as a value. Even if the ' is not intended to do bad things it is still quite common that people have it in their names or use it in texts, filenames, passwords etc. So always use the args version. (It is okay to build int and other primitives directly into whereClause though)

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

Sidebar

Related Questions

I'd like to use SQLiteCursor class to fetch data (not using Cursor class). This
I'm trying to query a word, for this I'm using the db.query method. But
i am trying to send multiple data using j query $.ajax method to my
Have a lot of unnecessary results using contains() method in my query. Don't tell
I am using this query: SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN
I am using Linq query and call method Like.. oPwd = objDecryptor.DecryptIt((c.Password.ToString()) it will
I'm building a drag and drop method, using query -onmousedown leading to -onmousemove (drag)
Using pubs If I want to join using query syntax I would do this.
I have a stored procedure that I call upon using 'core_read' and query method.
I have a sql query method using SQL data table adapters in .xsd file

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.