我的数据库包含问题,每个问题有三个选项。一切正常,但当我试图随机获取问题和选项时,它给了我错误:
我发现了与我的问题类似的问题,但它也没有用,
这是我的代码:
public List<Test> getTest() {
List<Test> MyList = new ArrayList<Test>();
myDB=this.getReadableDatabase();
Cursor cursor = myDB.rawQuery("SELECT * FROM " + My_Table + "ORDER BY RANDOM() LIMIT 1", null);
if (cursor.moveToFirst()) {
do {
Test newQ = new Test();
newQ .setId(cursor.getInt(0));
newQ .setTest(cursor.getString(1));
newQ .setAns(cursor.getString(2));
newQ .setO1(cursor.getString(3));
newQ .setO2(cursor.getString(4));
newQ .setO3(cursor.getString(5));
MyList .add(newQ );
} while (cursor.moveToNext());
}
return MyList ;}
当我将此行添加到我的代码中时:
ORDER BY RANDOM()LIMIT 1
活动无效。
答案 0 :(得分:4)
您还需要将SQL的ORDER BY
部分引用为Java字符串文字:
"SELECT * FROM " + My_Table + " ORDER BY RANDOM() LIMIT 1"