SQLite rawQuery选择标题为实数的列



我有一个表,它的列有数字:

表名"Valores"

id编号11 18 12.3
01 Juan 10 08 15
02罗莎23 51 61
03 Pepe 35 18 11

我想知道你在专栏中选择的任何名字的金额。第12.3列中Rosa的示例为61。我发表了以下声明:

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();
    }

以运行我得到的应用程序,结果为12.3(正确值61)。我犯了什么错?。谢谢你(对不起我的英语)

您忘记在查询中放入条件值(即Rosa)。

select "+ stColumna + " from Valores where Nombre = 'Rosa'
SELECT 12.3

选择数字文字CCD_ 1而不是列CCD_。要选择列,请引用标识符:

SELECT "12.3"

示例:

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

此外,您的where Nombre本身几乎毫无意义。

我找到了问题的解决方案:

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();
    }

谢谢你的帮助。

最新更新