Reputation: 1550
I'm following this tutorial http://www.sarahghaffary.com/android-quotes-generator-sample-application-using-alarmmanager-and-sqlite-database/, but I want to know how would I make it keep repeating the quotes even if runs out of quotes. I know I need to change the code below but no sure how. Only beginner, Any help would be awesome!
Code:
public String getRandomQuote(){
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + QUOTES_TABLE_NAME + " WHERE " + KEY_ISREAD + " = 0 ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
if(cursor.moveToFirst()){
db.execSQL("update " + QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 1 where "+ KEY_ID +" = "+ cursor.getString(0));
return cursor.getString(1);
}
return "Sorry, you ran out of quotes <img src="http://www.sarahghaffary.com/wp-includes/images/smilies/icon_biggrin.gif" alt=":D" class="wp-smiley"> ";
}
Upvotes: 0
Views: 358
Reputation: 2657
public String getRandomQuote(){
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + QUOTES_TABLE_NAME + " WHERE " + KEY_ISREAD + " = 0 ORDER BY RANDOM() LIMIT 1";
String UpdateAllQuery= "Update "+ QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 0";
Cursor cursor = db.rawQuery(selectQuery, null);
String quote = "";
if (cursor != null){
cursor.moveToFirst();
if (cursor.getCount() < 1){
db.execSQL(UpdateAllQuery);
cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
}
db.execSQL("update " + QUOTES_TABLE_NAME + " set " + KEY_ISREAD + " = 1 where "+ KEY_ID +" = "+ cursor.getString(0));
quote = cursor.getString(1);
return quote;
}else{
return "Sorry, you ran out of quotes <img src="http://www.sarahghaffary.com/wp-includes/images/smilies/icon_biggrin.gif" alt=":D" class="wp-smiley"> ";
}
Upvotes: 2