如何在sql中混洗列的数据?

时间:2013-09-01 12:45:16

标签: android sql sqlite

是否可以在sqlite android中随机播放列的数据? 我有两个列,我想在sqlite中对alph中的click event进行随机播放

num     alph
------------
 1       a
 2       b
 3       c
 4       d

这是我的代码

public class MainActivity extends Activity {
SQLiteDatabase db;
String a,c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db=openOrCreateDatabase("dat", MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS rk(num integer,alph char);");
    db.execSQL("INSERT INTO rk VALUES('1','a');");
    db.execSQL("INSERT INTO rk VALUES('2','b');");
    db.execSQL("INSERT INTO rk VALUES('3','c');");
    db.execSQL("INSERT INTO rk VALUES('4','d');");
    db.execSQL("INSERT INTO rk VALUES('5','e');");
}
public void disp(View view){
    EditText ed;
    ed=(EditText)findViewById(R.id.editText1);
    a=ed.getText().toString();
    Cursor  b=db.rawQuery("SELECT alph FROM rk WHERE num='a';",null);
    //System.out.println(b.getString(0));

    c=b.toString();
    System.out.println("a2");
    System.out.println(c);
    Builder builder=new AlertDialog.Builder(MainActivity.this);
    builder.setMessage(c);
    AlertDialog alert=builder.create();
    alert.show();
}

3 个答案:

答案 0 :(得分:1)

你的意思是随机排出那个?在MySQL中你可以通过以下方式实现:

select alph from rk order by rand() limit 0,1

我认为这个SQL也可以在SQLite中以相同或相似的形式工作。如果你想以随机顺序排列所有行,只需删除“limit 0,1”部分,如:

select alph from rk order by rand()

答案 1 :(得分:1)

SELECT * FROM table ORDER BY RANDOM()

答案 2 :(得分:0)

select * from tablename order by random() //记得rand()不是sqlite使用随机而是