private void createMusicTable(SQLiteDatabase db) {
db.execSQL("create table if not exists musictbl("
+ "_id integer primary key ,"
+ "title text not null,"
+ "data text not null,"
+ "size long,"
+ "duration int not null,"
+ "album text,"
+ "album_pic text,"
+ "album_id integer,"
+ "folder_path text,"
+ "date integer,"
+ "artist text,"
+ "artist_pic text,"
+ "recentiy_time integer"
+ ")");
}
这是一个保存音乐信息的表,现在要按照表结构中最后一个属性来作为条件进行查询,就是这个属性(recentiy_time)这个属性是用来记录时间的,主要作用是用来标示是否是最近播放过的歌曲, 它保存的值有两种,要么为0,要么就是时间。0表示不是最近播放过的歌曲,时间就是最近播放过的。这里用时间来表示是否是最近播放过的歌曲,主要是为了查询最近播放歌曲信息的时候,以这个属性为条件,并且还以它进行排序。请问各位大神这个该怎么写?
1.以recentiy_time 作为条件进行查询
2.还需以recentiy_time 它进行排序
还请麻烦大神们按照下面这个方法进行写:
/**
* 查询所有最近播放的歌曲
* @return
*/
public List<Music> getAllRecentiyMusic(){
List<Music> musics = new ArrayList<Music>();
SQLiteDatabase db = null;
Cursor c = null;
try {
db = helper.getReadableDatabase();
//这里该怎么写?
if (c != null && c.getCount() > 0) {
while (c.moveToNext()) {
Music music = new Music();
music.setId(c.getInt(c.getColumnIndex("_id")));
music.setDuration(c.getInt(c.getColumnIndex("duration")));
music.setAlbumId(c.getInt(c.getColumnIndex("album_id")));
music.setSize(c.getLong(c.getColumnIndex("size")));
music.setAlbumPic(c.getString(c.getColumnIndex("album_pic")));
music.setArtistPic(c.getString(c
.getColumnIndex("artist_pic")));
music.setTitle(c.getString(c.getColumnIndex("title")));
music.setData(c.getString(c.getColumnIndex("data")));
music.setAlbum(c.getString(c.getColumnIndex("album")));
music.setArtist(c.getString(c.getColumnIndex("artist")));
music.setRecentiyTime(c.getString(c.getColumnIndex("recentiy_time")));
musics.add(music);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(c, db);
}
return musics;
}
最后真诚感谢帮助我的人!谢谢!
4 个解决方案
#1
c = db.rawQuery("select _id,duration,album_id,size,album_pic,artist_pic,title,data,album,artist,recentiy_time from musictbl where recentiy_time <> 0",null);
排序可以对list进行排序,在music类里实现一下排序的接口就可以了吧
排序可以对list进行排序,在music类里实现一下排序的接口就可以了吧
#2
谢谢你!我改成这样应该也行吧?
String sql="select * from musictbl where recentiy_time <> 0 order by recentiy_time desc";
c = db.rawQuery(sql,null);
#3
从不结贴还有人回复
#4
我一直都结贴的啊!我哪有不结贴?
#1
c = db.rawQuery("select _id,duration,album_id,size,album_pic,artist_pic,title,data,album,artist,recentiy_time from musictbl where recentiy_time <> 0",null);
排序可以对list进行排序,在music类里实现一下排序的接口就可以了吧
排序可以对list进行排序,在music类里实现一下排序的接口就可以了吧
#2
谢谢你!我改成这样应该也行吧?
String sql="select * from musictbl where recentiy_time <> 0 order by recentiy_time desc";
c = db.rawQuery(sql,null);
#3
从不结贴还有人回复
#4
我一直都结贴的啊!我哪有不结贴?