How to get a random row from a database?

I am working on an application in which the first action gets a random value from the database and sends it to the next action as a string.

I know exactly how to do this in sql:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

After some attempts to use it in the application, I came to the following:

Cursor send = database.query(TABLE_NAME, new String[] { COLUMN_NAME }, null, null, null, null, "RANDOM() LIMIT 1");

Bundle b = new Bundle();
b.putString("description", send.getString(send.getColumnIndex(COLUMN_NAME)));

But I got an error on the last line after clicking the button to open the second action.


Added log:

05-22 22:14:24.202: E/AndroidRuntime(14259): FATAL EXCEPTION: main
05-22 22:14:24.202: E/AndroidRuntime(14259): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-22 22:14:24.202: E/AndroidRuntime(14259): at com.example.ListAction$1.onItemClick(ListAction.java:90)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView.performItemClick(AbsListView.java:1177)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2705)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.widget.AbsListView$1.run(AbsListView.java:3458)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Handler.handleCallback(Handler.java:605)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.os.Looper.loop(Looper.java:137)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at android.app.ActivityThread.main(ActivityThread.java:4507)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at java.lang.reflect.Method.invoke(Method.java:511)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-22 22:14:24.202: E/AndroidRuntime(14259):    at dalvik.system.NativeStart.main(Native Method)
+3
source share
2 answers

I think the problem is that you have to point the cursor to the first line. I did not find this in your code:

if (!send.moveToFirst()) {
    Log.d(TAG, "Cannot move to first row of the Cursor!");
    return;
}
+2
source

Use instead rawQuery:

database.rawQuery("SELECT column 
                   FROM table 
                   ORDER BY RANDOM() LIMIT 1",null);

You will get the same type cursor, but by executing the query, as usual, in sql.

, , , :

Cursor send =database.rawQuery("SELECT column 
                   FROM table 
                   ORDER BY RANDOM() LIMIT 1",null);
Bundle b = new Bundle();
b.putString("description", send.getString(send.getColumnIndex(COLUMN_NAME)));
+1

All Articles