android sqlite随机从数据库中获取数据 [英] android sqlite getting data from database randomly
本文介绍了android sqlite随机从数据库中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库包含问题,每个问题都有三个选项.一切正常,但是当我尝试随机获取问题和选项时,它给了我错误:
my database contains questions and each question has three options. everything work fine but when I try to get the questions and options randomly it it gives me errors:
我发现了与我的问题类似的问题,但它也不起作用,
I found similar questions to my problem but it also didn't work,
这是我的代码:
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 ;}
当我将这一行添加到我的代码中时:
when I add this line to my code:
ORDER BY RANDOM() LIMIT 1
ORDER BY RANDOM() LIMIT 1
该活动将不起作用.
推荐答案
您还需要将 SQL 的 ORDER BY
部分引用为 Java 字符串文字:
You need to quote the ORDER BY
part of the SQL as Java string literal as well:
"SELECT * FROM " + My_Table + " ORDER BY RANDOM() LIMIT 1"
这篇关于android sqlite随机从数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文