SQLite的(Android版) - 选择随机行SQLite、Android

2023-09-07 10:16:07 作者:冰冻的心没有太阳难融化了

我要返回游标的方法,但它不幸滋生出于某种原因NullPointerException异常。

这里的方法,有什么错呢?

 公共光标getRandom(字符串tableName值){        返回db.query(tableName值+ORDER BY RANDOM()LIMIT 1,新的String [] {KEY_ID,KEY_TEXT},NULL,NULL,NULL,NULL,NULL);    } 

解决方案

按声明的顺序是在错误的地方。

  SQLiteDatabase.query(字符串表,字符串[]栏,选择字符串,字符串[] selectionArgs两个,字符串GROUPBY,有字符串,字符串的OrderBy,字符串限制)。 
安卓存储sqlite之实现

看看在 API 。

I have a method to return a Cursor, however it unfortunately spawns a NullPointerException for some reason.

Here's the method, is there anything wrong with it?:

public Cursor getRandom(String tableName) {
        return db.query(tableName + " Order BY RANDOM() LIMIT 1", new String[] {KEY_ID, KEY_TEXT}, null, null, null, null, null);
    }

解决方案

The order by statement is in the wrong place.

SQLiteDatabase.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit).

Take a look at the API.