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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:01:36+00:00 2026-05-17T19:01:36+00:00

I have a table called person and it contains two fields, name (TEXT) and

  • 0

I have a table called person and it contains two fields, name (TEXT) and age (INT). I want to sort this table alphabetically but I can seem to get the sql statement to work. The statement i am using is:

SELECT name FROM person ORDER BY name;

I have tried three different coded versions, none of which work.

db.rawQuery("SELECT name FROM person ORDER BY name;", null);
db.execSQL("SELECT name FROM person ORDER BY name;", null);
db.execSQL("SELECT name FROM person ORDER BY name;");

rawQuery does absolutely nothing, no messages are displayed in LogCat. the first execSQL crashes with:

java.lang.IllegalArgumentException: Empty bindArgs

The second execsql crashes with:

android.database.sqlite.SQLiteException: unknown error: Queries cannot be performed using execSQL(), use query() instead.

I also tried using query() but I couldn’t make heads or tails of it. Help please.

  • 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-17T19:01:37+00:00Added an answer on May 17, 2026 at 7:01 pm

    Try:

    db.query("person", new String[] {"name"}, null, null, null, null, "name");
    

    As per the reference:

    public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 
    

    If for example you wanted both name and age you would use:

    db.query("person", new String[] {"name", "age"}, null, null, null, null, "name");
    

    If you wanted to select only names with “woody” in them you could use:

    db.query("person", new String[] {"name"}, "name=?", new String[] { "woody" }, null, null, "name");
    

    For processing the results you could do the following:

    Cursor c = db.query("person", new String[] {"name", "age"}, null, null, null, null, "name");
    
    startManagingCursor(c); 
    int nameColumn = c.getColumnIndex("name");
    int ageColumn = c.getColumnIndex("age");
    c.moveToFirst()) 
    do 
    {
        // Perform Logic
        Log.i('TEST INFO', 'Name: '.c.getString(nameColumn).' Age: '.c.getInt(ageColumn));
    }
    while (c.moveToNext());
    

    Amendment as per the comment: I am not quite sure why you would want to reorder the data in a database, but the following function should do what you want, you will probably need to modify to suite, it will get all entries (ordered), create a temp table, add the entries, drop the real table and rename the temp table to become the main table again:

    Doing this is very resource hungry – using a properly laid out SQL SELECT statement to order result as they are collected from the DB will use much less.

    private void orderDB()
    {
        Cursor c = db.query("person", new String[] {"name", "age"}, null, null, null, null, "name");
        startManagingCursor(c); 
    
        //Create a temp table:
        db.execSQL("CREATE TABLE IF NOT EXISTS tmp_person (_id integer primary key autoincrement, "
            + "name text not null, " 
            + "age integer);");
    
        // Get column ID's
        int nameColumn = c.getColumnIndex("name");
        int ageColumn = c.getColumnIndex("age");
    
        // Iterate through all entries
        c.moveToFirst(); 
        do 
        {
            if (c.getCount() >0)
            {
                ContentValues orderedValues = new ContentValues();
                orderedValues.put("name", c.getString(nameColumn));
                orderedValues.put("age", c.getInt(ageColumn));
                try
                {
                    // Insert the entry into a temp table
                    db.insert("tmp_person", null, orderedValues);
                }
                catch (SQLException e)
                {
                    Log.e("TEST INFO", e.toString());
                }
            }
        }
        while (c.moveToNext());
    
        // Drop the current person table:
        db.execSQL("DROP TABLE IF EXISTS person");
    
        // Rename the temp table to person:
        db.execSQL("ALTER TABLE tmp_person RENAME TO person");  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called OffDays, where weekends and holiday dates are kept. I
I have a Table called Product and I have the Table StorageHistory . Now,
I have a table called ApprovalTasks... Approvals has a status column I also have
I have a table called BlogPost which has a 1-to-many relationship with the Comment
i have a table called category in which i have main category ids and
Suppose I have a table called Companies that has a DepartmentID column. There's also
Say I have a table called myTable. What is the SQL command to return
Say I have a table called xml that stores XML files in a single
Right now I have a table called Campaigns that has many Hits, if I
I have a database table called Posts which stores all the information regarding an

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.