我的数据库中有一个表名 Answers,有 5 列 _id、b1、b2、b3、b4,我要做的就是在游标中随机获取列 b1、b2、b3、b4,我能够使用下一个代码按顺序将它们放在我的光标中:
public Cursor getAllAnswers(int n)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor curs;
curs=myData.rawQuery("SELECT b1, b2, b3, b4 FROM Answers WHERE _id='"+n+"';", null);
curs.moveToFirst();
myData.close();
return curs;
};
并试图让他们随机使用
curs=myData.rawQuery("SELECT b1, b2, b3, b4 FROM Answers WHERE _id='"+n+"' ORDER BY RANDOM() LIMIT 1;", null);
但不工作。我在这里先向您的帮助表示感谢
编辑显示完成由 Geobits 显示的随机列的正确方法(如果你也需要它):
public Cursor getAllAnswers(int n)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String query = "SELECT ";
boolean[] chosen = new boolean[]{false, false, false, false};
Random rand = new Random();
for(int i=0;i<4;i++)
{
if(i > 0)
query += ", ";
int c = rand.nextInt(4);
while(chosen[c])
c = rand.nextInt(4);
chosen[c] = true;
query += "b" + Integer.toString(c+1);
}
query += " FROM Answers WHERE _id='"+n+"';";
Cursor curs;
curs=myData.rawQuery(query, null);
curs.moveToFirst();
myData.close();
return curs;
};