I'm writing a small app to play music. My target is Android API level 7 and up.
The app should only list and play music that has been tagged with a specific set of genres chosen by the user via the preferences, e.g. "Pop", "Rock", "Hillbilly".
Right now I'm trying to find an efficient way to find all album ids and album names that contain tracks which are tagged with the chosen genres.
This is easy in SQL. The phone stores the media meta data in an sqlite database. I have found it on the phone's internal memory, copied it via adb and tried some sqlite queries on it. The resulting query is an SQL one-liner with a chain of joins and it works fine.
This seems to be more difficult in Java with the Android SDK, at least with API level 7 and up. I can use managedQuery on the MediaStore to do some limited SQL syntax on that same underlying database, but if I understand things correctly there is no JOIN and no DISTINCT when using this API.
Also I may have overlooked this, but MediaStore allows me to search for all tracks of one genre, but not for all tracks of several genres in a single query.
So this all gets a bit of manual work in java. The program now has to do several managedQuery() calls for each genre and the program has to join the results and make them distinct with java, outside of sqlite (where this work belongs).
Is this really how it has to be done? Is there no way to just send a raw query to the sqlite database file?
Thanks!
On the other hand, RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into objects. RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use RoomDatabase#query methods.
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.
Important Methods in SQLite DatabaseThis method will return the number of rows in the cursor. This method returns a Boolean value when our cursor is closed. This method returns the total number of columns present in our table. This method will return the name of the column when we passed the index of our column in it.
No you can not directly access the underlying structure of the database -- in fact you have no way to know the actual structure of the database, it has changed across platform versions. The only direct queries you can do are the ones provided through the MediaStore protocol of the content provider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With