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.
Try:
As per the reference:
If for example you wanted both name and age you would use:
If you wanted to select only names with “woody” in them you could use:
For processing the results you could do the following:
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.