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

  • Home
  • SEARCH
  • 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 table called emp . The table contains three columns empid , ename
Here's my problem. Suppose I have a table called persons containing, among other things,
I have table called Table1 with columns, col1 and col2 with col1 having weblinks
i have table called table1 and it looks like the below record type tran_ref_number
2 databases QF AND TK QF has the following: Imagine you have a table
I have an inheritance hierarchy with overlap. The system knows about People that can
I am trying to get dynamic select menu's working in my Rails application. I
I'm writing a JSF 2.0 application with Hibernate and Postgresql database. My problem is
Hey all, i am new at everything VB.net/ASP.net so i need some help with
I am getting 406 Not Acceptable error when submitting my form with jQuery. I

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.