Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android studio SQL-如何在字符串数组中返回数据_Java_Android_Sql_Sqlite_Android Sqlite - Fatal编程技术网

Java Android studio SQL-如何在字符串数组中返回数据

Java Android studio SQL-如何在字符串数组中返回数据,java,android,sql,sqlite,android-sqlite,Java,Android,Sql,Sqlite,Android Sqlite,我这里有一个SQL方法。我想返回字符串[]数组中的数据 我该怎么做呢 谢谢大家! public String[] getLikedSongs() { SQLiteDatabase db = this.getReadableDatabase(); String[] LikeSong; Cursor cursor = db.rawQuery(" SELECT " + COL_4 + " FROM " + Tabl

我这里有一个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;
    }

必须定义数组的长度,只有在光标获取所有行后才能执行此操作。然后将其长度设置为光标的行数。
然后在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;
}

非常感谢你!这就解决了!我非常感谢你的帮助:)