Select and calculate columns - Sqlite

Asked

Viewed 528 times

1

Hello...

//selecionar valores da tabela:

    public Cursor IMC(){
            Cursor cursor;
            String[] campos =  {"SELECT (peso/(altura*altura)) FROM medidas WHERE codigo = (SELECT codigo FROM medidas ORDER BY codigo DESC LIMIT 1)"};

            db = banco.getReadableDatabase();

            cursor = db.query("medidas", campos, null, null, null, null, null, null);

            if(cursor!=null){
                cursor.moveToFirst();
            }
            db.close();
            return cursor;
        }

In Sqlexpert the SELECT looking for only a column or a simple operation as weight+height works, but this in the code above returns 0. I’ve found that you are creating the database and saving the values.

//mostrar o resultado do SELECT em um TextView
public void resultadoIMC() {

                BancoController crud = new BancoController(getBaseContext());
                Cursor cursor = crud.IMC();


                TextView tv = (TextView) findViewById(R.id.txtresultado_imc);

                tv.setText(cursor.getString(cursor.getColumnIndex(String.valueOf(0))));


            }

In this code returns no value, even changing the SELECT to fetch the value of a single column (In Sqlexpert the SELECT works).

Does anyone have any idea what’s wrong???

1 answer

1

//você precisa abrir a conexão antes
db = banco.getReadableDatabase();

//salvando a query antes. Eu NÃO conferi se a query está correta
//só coloquei a maneira certa de fazer um SELECT
String query =  "SELECT (peso/(altura*altura)) FROM medidas WHERE codigo = (SELECT codigo FROM medidas ORDER BY codigo DESC LIMIT 1)";

//usando rawQuery, que é o correto
Cursor cursor = db.rawQuery(query,null);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.