0

I have a table whose columns have numbers:

Table name "Valores"

id Nombre 11 18 12.3
01 Juan 10 08 15
02 Rosa 23 51 61
03 Pepe 35 18 11

I want to know the amount you have chosen any name in the column. Example to Rosa in the column 12.3 is 61. I made the following statements:

columna = (EditText) findViewById(R.id.eT_columna);
valor = (EditText) findViewById(R.id.eT_valor);
String stColumna = columna.getText().toString();

    public void consulta (View v){

       //Determinación del valor
        Cursor fila_valores = bd_valores.rawQuery(
                "select "+ stColumna + " from Valores where Nombre", null);
        if (fila_valores.moveToFirst()) {
            valor.setText(fila_valores.getString(0));
        }
        bd_valores.close();
    }

to run the application I get as a result 12.3 (correct value 61). What is my mistake ?. Thank You (sorry for my English)

  • `from Valores where Nombre` you forget to put `from Valores where Nombre = 'Rosa'` – Randyka Yudhistira Feb 12 '15 at 02:49
  • you're right `"select "+ stColumna + " from Valores where Nombre="+" name "` , but still the error persists. the value stColumn = 12.3 still persists, does not recognize the column 12.3 and gives me result in the same value and not the correct value 61. – Pedro Alexis Feb 12 '15 at 20:52

3 Answers3

0

You forgot to put condition value (i.e. Rosa) in the query.

select "+ stColumna + " from Valores where Nombre = 'Rosa'
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0
SELECT 12.3

selects the numeric literal 12.3 and not the column 12.3. To select the column, quote the identifier:

SELECT "12.3"

Example:

sqlite> create table a("12.3");
sqlite> insert into a select 45.6;
sqlite> select 12.3 from a;
12.3
sqlite> select "12.3" from a;
45.6

Additionally, your where Nombre is pretty much meaningless as such.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I'm working on Android Studio with a database whose column names are available, but not recognized as such but as text, but also throws me the value of the column and not the value of the intersection with the name "Rosa ". any other suggestions? Thank You. – Pedro Alexis Feb 12 '15 at 20:59
0

I found the solution to the problem:

columna = (EditText) findViewById(R.id.eT_columna);
Nombre = (EditText) findViewById(R.id.eT_Nombre);
valor = (EditText) findViewById(R.id.eT_valor);
String stColumna = columna.getText().toString();
String stNombre = Nombre.getText().toString();

    public void consulta (View v){

       //Determinación del valor
        Cursor fila_valores = bd_valores.rawQuery(
                "select "+ '"' + stColumna +'"'+ " from Valores where Nombre=" + "'"+stNombre+"'", null);
        if (fila_valores.moveToFirst()) {
            valor.setText(fila_valores.getString(0));
        }
        fila_valores.close();
    }

thank you for your help.