SQLite (Android) - Selecting Random Row

一曲冷凌霜 提交于 2019-12-08 11:15:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!