android studio sql-如何返回字符串数组中的数据

kg7wmglp  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(378)

我这里有一个sql方法。我想返回字符串[]数组中的数据。
我该怎么做呢?
谢谢您!

public String[] getLikedSongs() {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] LikeSong;
        Cursor cursor = db.rawQuery(" SELECT " + COL_4 + " FROM " + Table_Name + " WHERE " + COL_4 + " IS NOT NULL", null);
        while (cursor.moveToNext()) {
            String note = cursor.getString(0);
        }
        cursor.close();
        db.close();
        return LikeSong;
    }
2mbi3lxu

2mbi3lxu1#

必须定义数组的长度,只有在光标获取所有行之后才能执行此操作。然后将其长度设置为光标的行数。
然后在while循环中设置数组的项:

public String[] getLikedSongs() {
    String[] LikeSong = null;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT " + COL_4 + " FROM " + Table_Name + " WHERE " + COL_4 + " IS NOT NULL", null);
    if (cursor.getCount() > 0) {
        LikeSong = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()) {
            LikeSong[i] = cursor.getString(0);
            i++;
        }
    }
    cursor.close();
    db.close();
    return LikeSong;
}

相关问题