How can I consult several elements of a database with the same identifier? -SQLite-Android Studio

1

I want to consult a SQLite database in Android Studio. Having a database of people, which has the fields age and name. What I want is to extract from the database all people who are 19 years old.

 SQLiteDatabase db = OpenHelper.getReadableDatabase();
    String buscar[] ={"19"};
    String campos[] = {Utilidad.CAMPO_NOMBRE};
    Cursor cursor = db.query("personas",campos,Utilidad.CAMPO_EDAD+"=?",buscar,null,null,null);

In the previous code you see what I programmed at first, but I understand that the first person I find at 19 is stored in the cursor, but if I wanted to store all the people who can appear at 19 years of age should do?

Thank you very much

    
asked by Joaquin Miranda 08.05.2018 в 21:08
source

1 answer

0

Review the method query ()

  

query (String table, String [] columns, String selection, String []   selectionArgs, String groupBy, String having, String orderBy, String   limit)

Where:

  

selection : String: a filter that declares the rows to be returned,   formatted as a clause WHERE SQL (excluding WHERE ). Happen   null will return all rows for the given table.

and

  

selectionArgs : String: You can include? 's in the selection, which will be replaced by the selectionArgs values, so that they appear in the   selection. The values will be ligados as strings.

Actually what you require would be done with the query that you show in your question , in this case you are looking for all records where the age field has a value "19", all values would be stored in the cursor.

If you want to obtain only the first element in the cursor, use the parameter:

  

limit : String: limits the number of rows returned by the query, formatted as a LIMIT clause. Passing null does not indicate any   LIMIT clause.

Cursor cursor = db.query("personas",campos,Utilidad.CAMPO_EDAD+"=?",buscar,null,null, "1" /* este es el parametro limit! */);
    
answered by 08.05.2018 в 21:17