コード例 #1
0
ファイル: music.go プロジェクト: zero2012/music
func (this *musicModel) QueryMusicById(musicId int) (*element.MusicInfo, error) {
	if DatabaseInstance().Open() != nil {
		return nil, errors.New("打开数据库失败")
	}
	defer DatabaseInstance().Close()
	rows, err := DatabaseInstance().DB.Query("select musicname, authorname, albumname, time, type from music where musicId = ?", musicId)
	if err != nil {
		fmt.Println(err)
		return nil, errors.New("查询失败")
	}
	if rows.Next() {
		var musicInfo *element.MusicInfo = &element.MusicInfo{}
		rows.Scan(&musicInfo.MusicName, &musicInfo.MusicAuthor, &musicInfo.AlbumName, &musicInfo.MusicTime, &musicInfo.SourceType)
		musicInfo.MusicId = musicId
		switch musicInfo.SourceType {
		case element.BaiduMusicSourceType:
			err = BaiduMusicModelInstance().FetchMusicInfo(musicInfo)
		case element.LocalMusicSourceType:
			err = MyMusicModelInstance().FetchMusicInfo(musicInfo)
		default:
			return nil, errors.New("SourceType Error!")
		}
		return musicInfo, err
	}
	return nil, errors.New("查询失败")
}
コード例 #2
0
ファイル: music.go プロジェクト: zero2012/music
func (this *musicModel) SaveMusic(musicInfo *element.MusicInfo) (int, error) {
	if musicInfo == nil && musicInfo.SourceType != element.BaiduMusicSourceType &&
		musicInfo.SourceType != element.LocalMusicSourceType {
		return 0, errors.New("参数错误")
	}
	if DatabaseInstance().Open() != nil {
		return 0, errors.New("打开数据库失败")
	}
	defer DatabaseInstance().Close()
	stmt, err := DatabaseInstance().DB.Prepare("insert INTO music(musicname, authorname, albumname, time, type) VALUES(?, ?, ?, ?, ?)")
	if err != nil {
		return 0, err
	}
	result, err := stmt.Exec(musicInfo.MusicName, musicInfo.MusicAuthor, musicInfo.AlbumName, musicInfo.MusicTime, musicInfo.SourceType)
	if err != nil {
		return 0, err
	}
	musicId, err := result.LastInsertId()
	musicInfo.MusicId = int(musicId)
	switch musicInfo.SourceType {
	case element.BaiduMusicSourceType:
		return BaiduMusicModelInstance().InsertMusic(musicInfo)
	case element.LocalMusicSourceType:
		return MyMusicModelInstance().InsertMusic(musicInfo)
	default:
		return 0, errors.New("SourceType Error")
	}
}