Java Android studio SQL-如何在字符串数组中返回数据
我这里有一个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
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;
}
非常感谢你!这就解决了!我非常感谢你的帮助:)