问题
I have a method to return a Cursor, however it unfortunately spawns a NullPointerException for some reason.
Here's the method, is there anything wrong with it?:
public Cursor getRandom(String tableName) {
return db.query(tableName + " Order BY RANDOM() LIMIT 1", new String[] {KEY_ID, KEY_TEXT}, null, null, null, null, null);
}
回答1:
The order by statement is in the wrong place.
SQLiteDatabase.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit).
Take a look at the API.
来源:https://stackoverflow.com/questions/12270802/sqlite-android-selecting-random-row