I found the below syntax for orderBy clause:
String orderBy = "SELECT * FROM " + TABLE + " ORDER BY " + COL_NAME + ";";
but it gives this error:
E/SQLite Exception: near "SELECT": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT _id, name, organization, phone, address, email, web_ad, inf, birthday FROM contact ORDER BY SELECT * FROM contact ORDER BY name;
I want to sort the entries in my database alphabetically according to their names using SQLiteDatabase.query() method.
String orderBy = "SELECT * FROM " + TABLE + " ORDER BY " + COL_NAME + ";";
try {
SQLiteDatabase db = this.getReadableDatabase();
return db.query( ContactHelper.TABLE, projection, null, null, null, null, orderBy);
}catch (Exception e){
Log.e("SQLite Exception", e.getMessage());
return null;
}
what's the mistake I'm making?
The orderBy
argument of the query()
method should contain only the column's name and not the full select
statement.
Change to this:
db.query( ContactHelper.TABLE, projection, null, null, null, null, COL_NAME);
In general that argument should contain the ORDER BY
clause of the statement excluding the ORDER BY
itself .
ASC
is optional. If you omit it then ASC
will be used.
If you want DESC
then you should write: COL_NAME + " DESC"
You can find more here: query method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.