jaisonDavis
jaisonDavis

Reputation: 1437

Sorting in SQLite

I have a database and I want to sort it. I came across several code that selects a sorted data but does not modify the database in itself. I came across.. So how do I sort a database?

Upvotes: 0

Views: 313

Answers (3)

pawelzieba
pawelzieba

Reputation: 16082

When creating database create index on column to have better query performance:

CREATE INDEX sorted_idx ON table_name(indexed_column ASC);

more about indexes: http://www.sqlite.org/lang_createindex.html

Then in code select using ORDER BY

db.rawQuery("SELECT * FROM table_name ORDER BY indexed_column ASC", null);

Upvotes: 2

rkosegi
rkosegi

Reputation: 14678

If you mean by 'sort a database' to simple sort a table,

  1. Create temp table as sorted select, eg you have unsorted table1 a you want to sort it by column1 then do this:

    CREATE TABLE temp_table1 
    AS SELECT * 
    FROM table1 
    ORDER BY column1;
    
  2. Drop the original table.

  3. Rename temp_table1 to table1.

Upvotes: 1

qianfg
qianfg

Reputation: 898

You don't sort tables in relational database.

You create index on them. And use ORDER BY in your query. That way your query result will be sorted.

Upvotes: 2

Related Questions