LoginSignup
0
0

More than 5 years have passed since last update.

ContentProviderでランダムに一件取得するクエリを叩くメモ

Last updated at Posted at 2013-12-07

ContentProviderを使ってランダムに一件だけデータ取得したい機会があったのでメモ。


Stack Overflow先生によると、こんな感じでSELECT文書けば良いとのことです。
http://stackoverflow.com/questions/1253561/sqlite-order-by-rand

SELECT * FROM table ORDER BY RANDOM() LIMIT 1;

RANDOM()はSQLiteの関数です。
http://www.sqlite.org/lang_corefunc.html


ContentProviderの場合はsortOrderのところに書いちゃえばOK。

Cursor c = context.getContentResolver().query(
        Hoge.CONTENT_URI,
        new String[]{Hoge._ID}, 
        null, 
        null, 
        "RANDOM() LIMIT 1");

おしまい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0