In my Android program, I'm trying to use a managedQuery with a selection argument, but I get an Sql exception.
07-26 19:30:44.364: INFO/Database(1163):
sqlite returned: error code = 1, msg = near ".": syntax error
Here is my example code
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.IS_MUSIC };
// Example without selection, works
String selection = null;
Cursor musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
// Example with selection, does not work
selection = "MediaStore.Audio.Media._ID = 1";
musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
Thanks, Niclas
The selection should be initialized like this:
selection = MediaStore.Audio.Media._ID+" = 1";
If you include the MediaStore.Audio.Media._ID in quotes the constant name is sent to SQLite to evaluate instead of actual column name _id.
Related
I'm trying to select multiple folders by querying the mediaStore. I can select a single folder by sing this piece of code:
String[] whereVal;
String selection;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " like ? " ;
whereVal = new String[]{"%Folder%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
However when I try to select multiple folders mediaStore returns nothing.
Code:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " IN(?,?)" ;
whereVal = new String[]{"%Top 20 English%","%Audio%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
Care to help me out? Thank in advance.
PS: I don't want to use OR operator.
I am trying to get list of mp3 files from a particular folder.
As of now, I am using contentResolver.query for getting the music from my phone.
Like this:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
ContentResolver contentResolver = context.getContentResolver();
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);
I am looking for solution to scan only a particular folder. How do I do this using contentResolver?
Thanks!
Finally figured it out myself. Hope it will help others.
Instead of this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
I used this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%MyMusicFolder%"}, null);
I am Using Android's builtin Content Provider android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI.
Now I have
String[] sarray = {"a" , "b" , "c" , "d"};`
String[] projection = {MediaStore.MediaColumns._ID , MediaStore.MediaColumns.DATA};
String selection = ?????? // My qestion
cursor = getContentResolver().query(uri, projection, null,
null , order);
So how do I write my cursor line so that I select All BUT NOT saray FROM MediaStore.MediaColumns._ID column.
Try this:
String[] sarray = {"a" , "b" , "c" , "d"};
String[] projection = {MediaStore.MediaColumns._ID , MediaStore.MediaColumns.DATA};
String selection =""+MediaStore.MediaColumns._ID+" !=? AND "+MediaStore.MediaColumns._ID+"" +
" !=? AND "+MediaStore.MediaColumns._ID+" !=? AND "+MediaStore.MediaColumns._ID+" !=?";
cursor = getContentResolver().query(uri, projection, selection,
sarray , order);
I have song on SD card and I'm trying to get title of that song:
File f = new File(filename);
String[] projection = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION};
Cursor musiccursor = managedQuery(Uri.fromFile(f),
projection, null, null, null);
musicTV.setText(musiccursor.getString(3));
but in last line i've got NullPointerException.
Thanks for any advice.
if you are using a cursor, when you are getting some content you have to move the cursor to the first element, because the cursor is pointing before the first element.
Try using:
Cursor musiccursor = managedQuery(Uri.fromFile(f), projection, null, null, null);
cursor.moveToFirst();
musicTV.setText(musiccursor.getString(3));
Have you seen this which gets you 90% there, then this will get you the rest of the way.
Query Device
Init Cursor
Traverse Cursor
The hard part is understanding the abstract nature of columns. Just remember it's a database table and can be visualized as a spreadsheet. DON'T FORGET CATCH STATEMENTS!
I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );