7

I am working on Android with the query() method of the ContentResolver class and it accepts 6 parameters, (uri,projection,selection,selectionArgs,sortOrder,cancellationSignal) for selection and projection parameters.

I am little bit confused with the selection and projection part of query.

In below example, what is selection and project part in the query?

select * from Person
where name='zeus';
gnat
  • 21,442
  • 29
  • 112
  • 288
  • [Sharing your research helps everyone](http://meta.programmers.stackexchange.com/questions/6559/why-is-research-important). Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see [ask] – gnat Jul 04 '14 at 07:25
  • Actually I am working on Android with the query() method of the ContentResolver class and it accepts the 6 parameters, (uri,projection,selection,selectionArgs,sortOrder,cancellationSignal) for selection and projection parameters I have asked this quesion – Mohammed Rampurawala Jul 04 '14 at 07:38
  • 1
    A very well explanation given [here](https://stackoverflow.com/a/1031101/2624806). – CoDe Jul 23 '17 at 23:28

3 Answers3

12

Selecting means choosing some records from a table and leaving others out.

Projecting means choosing some columns from each record and leaving others out.

Therefore, your query performs selection (records with name='zeus' are chosen, but others are rejected) but not projection (those records that are chosen are returned with all of their columns).

Broadly, the select keyword performs projection and the where keyword performs selection. (The fact that select is a language feature for choosing columns rather than rows, i.e. it performs projection, not selection, is unfortunate, but the syntax is far too established now to be corrected.)

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
-1

The projection refers to the columns you want back (the bit after SELECT and before FROM), the selection refers to the limiting of the records (the bit after WHERE and before ORDER BY). For Android, the projection variable should be something like:

new String[] {"name", "age"}

The selection variable should have the WHERE clause with the values replaced by question marks. The values go in the selectionArgs. So selection in your example would be:

"name=?"

and selectionArgs would be:

new String[] {"zeus"}

ojf
  • 19
  • 1
  • 4
-2

Projection = columns of record you want to fetch... Selection = puts condition to select specific rows selectionArgs = the condition by which rows are filtered..

in above example...

projection = *    i.e. all columns
selection = name i.e user want to filter records according to this column
selectionArgs = zesus  i.e records related to zesus are fetched. 

example..

SELECT  **DISTINCT _id, document_type_name** 
FROM metadata 
WHERE *(instrument_name=?)* 
ORDER BY document_type_name ASC
Dan Pichelman
  • 13,773
  • 8
  • 42
  • 73