TransWikia.com

Invalid token SELECT

Stack Overflow Asked by Virendra Varma on July 31, 2020

Hi I am trying to get the content of bucket Id using content resolver in android Q I am getting this Error

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #7
Process: com.dev.newtermain, PID: 13048
java.lang.IllegalArgumentException: Invalid token SELECT
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:472)
    at android.content.ContentResolver.query(ContentResolver.java:1183)
    at android.content.ContentResolver.query(ContentResolver.java:1115)
    at android.content.ContentResolver.query(ContentResolver.java:1071)

My Selection query is

selection = "bucket_id = ?) UNION SELECT _data, date_added, 0 as isImage FROM video WHERE (bucket_id = ?";
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
selectionArgs = new String[]{bucketIdString};
String[] projection = new String[]{
            MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_ADDED, MediaStore.Video.VideoColumns.DATE_ADDED
    };
Cursor cur = context.getContentResolver()
            .query(uri, projection, selection, selectionArgs, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC");

Any idea how I can fix this query

2 Answers

The selection parameter in ContentResolver::query does only support WHERE clauses (without the WHERE keyword). docs

Your approach is including the UNION clause in the selection which is invalid. If you need a union, you may have to do two separate queries and combine the two results yourself.

EDIT

For your specifc case the selection should be defined as follows

selection = "bucket_id = ?"

Correct answer by derpirscher on July 31, 2020

check out this selection

"SELECT _data, date_added, 0 as isImage FROM video WHERE bucket_id = ?";
  • removed bucket_id = ?) on start - this is just wrong, not proper format, doesn't fit to beginning of query...
  • removed following UNION as there is no union two selectors at all, just simple query for single video table
  • at the end removed unneeded opening bracket (WHERE (bucket_id = ? to WHERE bucket_id = ?)

but note that query is encapsulated and won't accept full single-string sqlite query, you have to split it for parts. variable selection would be "bucket_id = ?" for matching selectionArgs

also note that projection is a bit weird...

String[] projection = new String[]{
        MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_ADDED, MediaStore.Video.VideoColumns.DATE_ADDED
};

first two values are using MediaStore.Images instead of MediaStore.Video and all three doesn't match params in selection SELECT _data, date_added, 0 as isImage FROM - I see two params and one probably not needed static value

edit: I've just noticed you are selecting by Images - MediaStore.Images.Media.EXTERNAL_CONTENT_URI - but selection is pointing on video column... also I doubt that bucket_id column exists in MediaStore database, there is no such value in static declarations of columns... I would suggest you read a bit about sql and querying, because your snippet looks like every line is comming from another piece of code....

Answered by snachmsm on July 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP