在android sql中每天查询随机项

时间:2014-12-28 15:04:55

标签: android sqlite random

我正在开发一个移动应用程序,其中包含数千条记录,具有当天的单词功能。问题是sqlite不支持播种随机函数,如mysql在此示例查询中所做的那样

SELECT * 
  FROM word_table 
 ORDER BY RAND('. date("mdY") .') LIMIT 1

以下是使用

的代码

EntriesDataSource.java

public void insertWordForTheDayInfo(){
    String insert_columnd_id = "INSERT INTO " + EntriesDBOpenHelper.TABLE_WORD_OF_THE_DAY_TRACKER + "("+ EntriesDBOpenHelper.COLUMN_ID + ") VALUES (1)";
    database.execSQL(insert_columnd_id);

    //get the current date first
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
    String date = dateFormat.format(calendar.getTime());

    //get the entry id first
    String entry_id = "SELECT * FROM " + EntriesDBOpenHelper.TABLE_ENTRIES + " ORDER BY RANDOM() LIMIT 1";
    Cursor oldCursor = database.rawQuery(entry_id, null); 
    oldCursor.moveToFirst();

    int id = oldCursor.getInt(oldCursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_ID));

    String insert_data = "UPDATE " + EntriesDBOpenHelper.TABLE_WORD_OF_THE_DAY_TRACKER + " SET " + 
                        EntriesDBOpenHelper.COLUMN_CURRENT_DATE + " ='" + date + "', " + EntriesDBOpenHelper.COLUMN_ENTRY_ID + "= " + id + " WHERE " + EntriesDBOpenHelper.COLUMN_ID + " = 1"; 

    database.execSQL(insert_data);
}


public int retrieveWordForTheDayInfo(){
    //get the current date first
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
    String date = dateFormat.format(calendar.getTime());

    //String sql = "SELECT * FROM " + EntriesDBOpenHelper.TABLE_WORD_OF_THE_DAY_TRACKER + " WHERE " + EntriesDBOpenHelper.COLUMN_CURRENT_DATE + " = '" + date + "'";
    String sql = "SELECT * FROM " + EntriesDBOpenHelper.TABLE_WORD_OF_THE_DAY_TRACKER + " WHERE " + EntriesDBOpenHelper.COLUMN_ID + " = 1";
    Cursor cursor = database.rawQuery(sql, null);
    cursor.moveToFirst();

    int id = cursor.getInt(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_ENTRY_ID));

    return id;
}


public Entry wordForTheDay(){
    String query = "SELECT * FROM " + EntriesDBOpenHelper.TABLE_ENTRIES + " WHERE " + EntriesDBOpenHelper.COLUMN_ID + " = " + 1;
    Cursor cursor = database.rawQuery(query, null);

    Entry entry = new Entry();

    if(cursor.moveToFirst()){
        do{
            entry.setId(cursor.getLong(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_ID)));
            entry.setWord(cursor.getString(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_WORD)));
            entry.setAudioUrl(cursor.getString(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_AUDIO_URL)));
            entry.setCategory(cursor.getString(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_CATEGORY)));
            entry.setSubCategory(cursor.getString(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_SUBCATEGORY)));
            entry.setMeaning(cursor.getString(cursor.getColumnIndex(EntriesDBOpenHelper.COLUMN_MEANING)));
        }while(cursor.moveToNext());
    }

    Log.i(LOGTAG, "wordForTheDay(). Total entry: " + cursor.getCount() + " rows");
    return entry;
}

WordOfTheDayFragment.java(onCreate)

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
    listener = new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            changeLayout();
        }
    };

    settings.registerOnSharedPreferenceChangeListener(listener);

    datasource = new EntriesDataSource(getActivity());
    datasource.open();

    //insert date and id
    datasource.insertWordForTheDayInfo();

    //retrieve entry id
    int entry_id = datasource.retrieveWordForTheDayInfo();

    //retrieve entry info
    entry = datasource.wordForTheDay(entry_id);
}

1 个答案:

答案 0 :(得分:-1)

考虑让word_table具有字段,ID(1,2,... n)和单词。然后计算应用中的随机ID并使用:

SELECT word
  FROM word_table
 WHERE id=<random id>

如果您使用Java作为应用程序,则可以使用

等代码生成randomId
Random randomGenerator = new Random();
int RandomId = randomGenerator.nextInt(numRecords);

使用如下查询获取记录数(numRecords)的地方:

SELECT count(1)
  FROM word_table

从长远来看,我还建议你创建SQL函数来调用你的数据库操作。这样您就可以根据需要调整表结构,而无需重新编码应用程序。

如果你想建立一个新的&#34;随机&#34;每天的项目,您可以创建一个单独的表(一行),其中包含时间戳和随机ID。您可以创建一个传递random_id的函数 - 如果日期仍在同一天,它将使用当前存储的随机ID,或者使用新的random_id并覆盖特殊表中的日期,id