0

When I click on any tab (Album tab/Artist tab/Folder tab) it shows me this SQLite Exception. It seems confusing because when I run this project on my physical device (API level 26) it is throwing this Sqlite exception error but When I run this project on Android studio's virtual device (API level 31) it is working fine. I don't know how to resolve this issue. Maybe it is caused bcoz of different API levels of devices. I have shared the drive link

This is the screen recording When I use the app on a virtual device. It's working fine https://drive.google.com/file/d/1YnllFVQCJWUasPLTD93ljBTrz1bEV5Fi/view?usp=sharing

And this is the screen recording of my physical device .which throw me an error https://drive.google.com/file/d/1cChl_S1EXSUVUjspxIgPFggY-eb76i3X/view?usp=sharing

This is my AllAlbum.kt tab code

@Suppress("DEPRECATION")
class AllAlbums : Fragment() {
lateinit var albumRv: RecyclerView

lateinit var albumAdapter: AlbumAdapter
lateinit var songSearch: SearchView
lateinit var albumFilter: ImageButton

companion object {
    var albumList: ArrayList<AlbumsModal> = ArrayList()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view: View = inflater.inflate(R.layout.fragment_all_albums, container, false)

    songSearch = view.findViewById(R.id.albumSearch)
    albumFilter = view.findViewById(R.id.listFilter)
    songSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {

            return false;
        }

        override fun onQueryTextChange(newText: String?): Boolean {

            albumAdapter.getFilter().filter(newText)
            return false;
        }

    })

    //bottomsheet
    albumFilter.setOnClickListener {
        openBottomSheetForFilter()
    }

    albumBackgroundTask().execute()
    return view
}


inner class albumBackgroundTask : AsyncTask<String, Void, String>() {
    override fun doInBackground(vararg params: String?): String {
        if (albumList.isEmpty()) {
            getAllAlbums()
        }

        return "dataFetched"
    }

    override fun onPostExecute(result: String?) {
        super.onPostExecute(result)
        albumRv = view!!.findViewById(R.id.albumRv)

        albumRv.layoutManager = GridLayoutManager(requireContext(), 2)
        albumAdapter = AlbumAdapter()
        var gridLayoutDecorationclass = GridLayoutDecoration()

        albumRv.addItemDecoration(decorationLayout(2, 3, true))
        albumRv.adapter = albumAdapter

        albumAdapter.addAlbums(requireContext(), albumList)
        //   Toast.makeText(context, albumList.size.toString(), Toast.LENGTH_SHORT).show()

    }

}

private fun getAllAlbums() {
    val uri: Uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI
    val projection = arrayOf(
        MediaStore.Audio.AlbumColumns.ALBUM,
        MediaStore.Audio.AlbumColumns.ALBUM_ART,
        MediaStore.Audio.AlbumColumns.ARTIST,
        MediaStore.Audio.AlbumColumns.ALBUM_ID,
        MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
    )
    val c: Cursor? = context?.contentResolver?.query(
        uri,
        projection,
        null,
        null,
        MediaStore.Audio.AlbumColumns.ALBUM + " ASC"
    )

    if (c != null) {
        while (c.moveToNext()) {
            // Toast.makeText(requireContext(),"fol: "+c.columnNames,Toast.LENGTH_SHORT).show()
            val albumsModal: AlbumsModal
            val albumname: String = c.getString(0)
            val artistname: String = c.getString(2)
            val albumid: String = c.getString(3)
            val release: String = c.getString(4)

            val albumIdc =
                c.getLong(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)).toString()
            val uri = Uri.parse("content://media/external/audio/albumart")
            val artUri = Uri.withAppendedPath(uri, albumIdc).toString()

            albumsModal =
                AlbumsModal(
                    albumName = albumname,
                    albumArt = artUri,
                    artistName = artistname,
                    albumId = albumid,
                    release = release,
                )

            albumList.add(albumsModal)
        }
           c.close()
    }

}

This is my Database code

class DataBase(context: Context) :
SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
    private val DATABASE_VERSION = 1
    private val DATABASE_NAME = "LatticeMusicApplic.db"
    private val FAV_TABLE = "FavouriteTable"
    private val PLAYLIST_TABLE = "playlisttable"
    private val PLAYER_TRACK_REC_TABLE = "playertrackrecordtable"
    private val KEY_ID = "id"

    private var songName = "songname"
    private var songPicture = "songpicture"
    private var songArtist = "songartist"
    private var songAlbum = "songalbum"
    private var songPath = "songpath"

    private var playListName = "playlistname"
    private var playListPic = "playlistpic"
    private var playListPath = "playlistpath"
    private var playListDate = "playlistdate"

    private var trackName = "trackname"
    private var trackPath = "trackpath"
    private var trackPic = "trackpic"
    private var trackListningTime = "tracklisteningtime"
    private var trackListningCount = "tracklistningcount"

}

val con = context
override fun onCreate(db: SQLiteDatabase) {
    val CREATE_FAVOURITE_TABLE = ("CREATE TABLE " + FAV_TABLE + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + songName + " TEXT,"
            + songPicture + " TEXT,"

            + songPath + " TEXT"
            + ")")

    val CREATE_PLAYLIST_TABLE = ("CREATE TABLE " + PLAYLIST_TABLE + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + playListName + " TEXT"
            + ")")

    val CREATE_TRACK_REC_TABLE = ("CREATE TABLE " + PLAYER_TRACK_REC_TABLE + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + trackName + " TEXT,"
            + trackPath + " TEXT,"
            + trackPic + " TEXT,"
            + trackListningTime + " TEXT,"
            + trackListningCount + " TEXT"
            + ")")
    db.execSQL(CREATE_TRACK_REC_TABLE)
    db.execSQL(CREATE_FAVOURITE_TABLE)
    db.execSQL(CREATE_PLAYLIST_TABLE)

}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
    db.execSQL("DROP TABLE IF EXISTS " + FAV_TABLE)
    db.execSQL("DROP TABLE IF EXISTS " + PLAYLIST_TABLE)
    db.execSQL("DROP TABLE IF EXISTS " + PLAYER_TRACK_REC_TABLE)

    onCreate(db)

}

fun inertIntoFavourite(sName: String, sPicture: String, sPath: String): Long {
    val db = this.writableDatabase
    val contentValues = ContentValues()

    contentValues.put(songName, sName)
    contentValues.put(songPicture, sPicture)
    contentValues.put(songPath, sPath)

    //  Toast.makeText(con, "checkPath: " + sPath, Toast.LENGTH_SHORT).show()
    val success = db.insert(FAV_TABLE, null, contentValues)
    db.close()
    return success
}

fun getLastInertedId(): Int? {
    var id: Int? = null
    val db = this.writableDatabase
    var cursor = db.rawQuery("SELECT * FROM $PLAYLIST_TABLE", null)

    if (cursor != null) {
        if (cursor.moveToLast()) {
            id = cursor.getInt(0)
        }
    }

    return id
}

fun checkSongPth(path: String): Long {
    var res: Long = -1L
    var checkPath: String = ""
    val db = this.writableDatabase

    var cursor = db.rawQuery("SELECT * FROM $FAV_TABLE WHERE $songPath =?", arrayOf(path))

    if (cursor != null) {

        if (cursor.moveToFirst()) {

            //  checkPath = cursor.getString(3) //to get id, 0 is the column index
            if (path.equals(checkPath)) {

            }
            res = cursor.getLong(0)
            // Log.d("checkPathDb","checkif:  $res")
        }
    return res
}

fun getAllData(): ArrayList<FavouriteModal> {
    val stdList: ArrayList<FavouriteModal> = ArrayList()
    val selectQuery = "SELECT * FROM $FAV_TABLE"
    val db = this.readableDatabase
    val cursor: Cursor?

    try {
        cursor = db.rawQuery(selectQuery, null)
    } catch (e: Exception) {
        e.printStackTrace()
        db.execSQL(selectQuery)
        return ArrayList()
    }

    var songId: Int
    var songName: String
    var songPicture: String
    var songPath: String

    if (cursor.moveToFirst()) {
        do {
            var data: FavouriteModal
            songId = cursor.getInt(0)
            songName = cursor.getString(1)
            songPicture = cursor.getString(2)
            songPath = cursor.getString(3)

            data = FavouriteModal(
                sId = songId.toString(),
                sPath = songPath,
                sName = songName,
                sIcon = songPicture
            )
            stdList.add(data)
        } while (cursor.moveToNext())
    }

    return stdList
}

This is the Error showing when I clicked on album tab(in physical device)

  java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:353)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: android.database.sqlite.SQLiteException: no such column: album_id (code 1): , while compiling: SELECT album, album_art, artist, album_id, numsongs FROM album_info ORDER BY album ASC
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
    at android.content.ContentResolver.query(ContentResolver.java:858)
    at android.content.ContentResolver.query(ContentResolver.java:779)
    at android.content.ContentResolver.query(ContentResolver.java:737)
    at com.example.app.AllFragments.AllAlbums.getAllAlbums(AllAlbums.kt:111)
    at com.example.app.AllFragments.AllAlbums.access$getAllAlbums(AllAlbums.kt:25)
    at com.example.app.AllFragments.AllAlbums$albumBackgroundTask.doInBackground(AllAlbums.kt:78)
    at com.example.app.AllFragments.AllAlbums$albumBackgroundTask.doInBackground(AllAlbums.kt:75)

This is the Error when I click on folder tab (in physical device)

 Caused by: android.database.sqlite.SQLiteException: no such column: relative_path (code 1): , while compiling: SELECT relative_path, bucket_display_name, date_added FROM audio ORDER BY bucket_display_name DESC
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
    at android.content.ContentResolver.query(ContentResolver.java:858)
    at android.content.ContentResolver.query(ContentResolver.java:779)
    at android.content.ContentResolver.query(ContentResolver.java:737)
    at com.example.app.AllFragments.AllFolders.getAllFolderWithPath(AllFolders.kt:107)
    at com.example.app.AllFragments.AllFolders.access$getAllFolderWithPath(AllFolders.kt:23)
    at com.example.app.AllFragments.AllFolders$AllFolderBackgroundTask.doInBackground(AllFolders.kt:74)
    at com.example.app.AllFragments.AllFolders$AllFolderBackgroundTask.doInBackground(AllFolders.kt:70)

1 Answers1

-1

You can create if/else for your API version. You can get your answer here : album_info does not contain album_id column error

Kishan Mevada
  • 662
  • 1
  • 6
  • 17