AnswerBun.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!

Related Questions

Efficiently Zip multiple list in python/pandas

3  Asked on December 14, 2020 by sunni

   

Pandas Excel groupby/count

1  Asked on December 14, 2020 by nathaniel

   

How to call a php function in javascript using Ajax?

2  Asked on December 14, 2020 by mr-skan

     

Angular class names based on item values

2  Asked on December 14, 2020 by mafortis

     

is there any way to give name object?

1  Asked on December 14, 2020 by beginner-coder

 

HTML hidden not submitting

2  Asked on December 14, 2020 by connor-gaymon

     

how to find and remove array using jquery

1  Asked on December 13, 2020 by sultan-achmad

   

Laravel GraphQL and Sanctum – how to combine it?

0  Asked on December 13, 2020 by kamilon123s

     

How to save multiple figures built with a loop and mapplot packages in r?

1  Asked on December 13, 2020 by juan-carlos-rubio-polania

         

Pick from both sides?

2  Asked on December 13, 2020 by helloworld

       

how to return only the True values?

2  Asked on December 13, 2020 by hnakashima96

       

multiple image galleries on one page

2  Asked on December 13, 2020 by flw

     

System.MissingMethodException: Method not found?

34  Asked on December 13, 2020 by user603007

         

Use auto for only one variable with structured binding

3  Asked on December 13, 2020 by hkon-hgland

   

What does the & symbol mean here and whats going on here?

4  Asked on December 13, 2020 by aarat-chopra

   

How to get suffix of filename in javascript quickly?

2  Asked on December 13, 2020 by daniel-stephens

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP