'Kotlin Android studio: How to get a random row of data from SQLite?

Im using an array list in kotlin and using the sqlite line

"SELECT * FROM $TBL_WORD ORDER BY RANDOM() LIMIT 1"

but it gives error Caused by: android.database.sqlite.SQLiteException: near "RANDOM": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM tbl_word ORDER BY RANDOM() LIMIT 1

How do i fix this?

Random word method

fun getRandomWord():ArrayList<WordModel>{//
    val wordList: ArrayList<WordModel> = ArrayList()
    val selectQuery = "SELECT * FROM $TBL_WORD ORDER BY RANDOM() LIMIT 1"
    val db = this.readableDatabase

    val cursor: Cursor?

    try {
        cursor = db.rawQuery(selectQuery, null)
    } catch (e: Exception) {
        e.printStackTrace()
        db.execSQL(selectQuery)
        return ArrayList()
    }
    var id: Int
    var date: String
    var engword: String
    var jpword: String
    var time: Int
    var answer: Int

    if (cursor.moveToFirst()) {
        do {
            id = cursor.getInt(cursor.getColumnIndexOrThrow("id"))
            date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
            engword = cursor.getString(cursor.getColumnIndexOrThrow("englishWord"))
            jpword = cursor.getString(cursor.getColumnIndexOrThrow("jpnWord"))
            time = cursor.getInt(cursor.getColumnIndexOrThrow("time"))
            answer = cursor.getInt(cursor.getColumnIndexOrThrow("answer"))

            val word = WordModel(
                id = id,
                date = date,
                engword = engword,
                jpword = jpword,
                time = time,
                answer = answer
            )
            wordList.add(word)
        } while (cursor.moveToNext())
    }
    return wordList
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source