How to view query result in SQLite?

0

To view tables I use a plugin called QuestoIdPlugin and it works fine, but to see query results, is there anything?

    
asked by anonymous 13.07.2015 / 19:39

3 answers

1
Cursor cursor;

select_query = "SELECT a,b,c FROM table";

        cursor = db.rawQuery(select_query, null);

        if(cursor.getCount()>0){
           while (cursor.moveToNext()) { //se a select devolver várias colunas
            cursor.moveToFirst();

According to the data type of the columns that the query will return, you use the following command to get the column (in this case if double, if String is getString ...)

cursor.getDouble(cursor.getColumnIndex(nome_da_Coluna)))
} //fim do while
} //fim do if
    
13.07.2015 / 22:30
0

Use a cursor to return the results of your QUERY

Example:

    Cursor c = database.rawQuery("SELECT * FROM nome_da_tabela", null);
    c.moveToFirst();

With this you can use "c" to return your results.

    
13.07.2015 / 19:47
0

You can use the class DatabaseHelper by creating a db object using helper.getReadableDatabase() , as shown in the example below.

SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, tipo_viagem, destino, " +
"data_chegada, data_saida, orcamento FROM viagem",
null);
cursor.moveToFirst();

viagens = new ArrayList<Map<String, Object>>();

for (int i = 0; i < cursor.getCount(); i++) {
    Map<String, Object> item =new HashMap<String, Object>();

    String id = cursor.getString(0);
    int tipoViagem = cursor.getInt(1);
    String destino = cursor.getString(2);
    long dataChegada = cursor.getLong(3);
    long dataSaida = cursor.getLong(4);
    double orcamento = cursor.getDouble(5);
    item.put("id", id);

    if (tipoViagem == Constantes.VIAGEM_LAZER) {
        item.put("imagem", R.drawable.lazer);
    } else {
        item.put("imagem", R.drawable.negocios);
    }

    item.put("destino", destino);

    Date dataChegadaDate = new Date(dataChegada);
    Date dataSaidaDate = new Date(dataSaida);

    String periodo = dateFormat.format(dataChegadaDate) +
    " a " + dateFormat.format(dataSaidaDate);

   item.put("data", periodo);
   double totalGasto = calcularTotalGasto(db, id);
   item.put("total", "Gasto total R$ " + totalGasto);
   double alerta = orcamento * valorLimite / 100;
   Double [] valores =
   new Double[] { orcamento, alerta, totalGasto };
   item.put("barraProgresso", valores);
   viagens.add(item);
   cursor.moveToNext();
}
cursor.close();

Now you just need to adapt your need.

    
16.07.2015 / 02:56