Obtain values of a query prepared in sqlite

0

Trying to run the program generates the following error:

  

Query.val: 41.24-41.34: error: The name 'name' does not exist in the context of 'Sqlite.Statement?'

The line where the error is generated is at the moment of obtaining the values:

while (stmt.step () == Sqlite.ROW) {
    string val = stmt.column_name ("nombre");
    stdout.printf ("servicio: %s\n", val);
}

How could I get the values from my query?

Here the code:

using Gtk;
using Sqlite3;
using AppIndicator;

public class IndicatorExample {
    public static int main(string[] args) {
        Gtk.init(ref args);

        Sqlite.Database db;
        string errmsg;

        var indicador = new Indicator("com.iuninefrendor", "indicator-messages",
                                      IndicatorCategory.APPLICATION_STATUS);

        indidator.set_status(IndicatorStatus.ACTIVE);
        indidator.set_attention_icon("indicator-messages-new");

        var menu = new Gtk.Menu();

        var item = new Gtk.MenuItem.with_label("Consultar servicios");
        item.activate.connect(() => {
            indicator.set_status(IndicatorStatus.ATTENTION);

            int ec = Sqlite.Database.open ("servicios.db", out this.db);
            if (ec != Sqlite.OK) {
                stderr.printf ("Ocurrio un error al abrir la db: %d: %s\n", this.db.errcode (), this.db.errmsg ());
                return -1;
            }

            Sqlite.Statement stmt;

            const string consulta = "SELECT * FROM servicios;";
            ec = db.prepare_v2 (consulta, consulta.length, out stmt);
            if (ec != Sqlite.OK) {
                stderr.printf ("Error en la consulta: %d: %s\n", this.db.errcode (), this.db.errmsg ());
                return -1;
            }

            int columnas = stmt.column_count ();
            while (stmt.step () == Sqlite.ROW) {
                    string val = stmt.column_name ("nombre");
                    stdout.printf ("servicio: %s\n", val);
                }
            }
        });

        item.show();
        menu.append(item);

        indicator.set_menu(menu);
        Gtk.main();
        return 0;
    }
}
    
asked by iuninefrendor 26.02.2018 в 00:57
source

1 answer

1

You can not take a column by its name, you have to indicate the type of value it is and the index of the column, in order to get a column by its name you should do something like this

while (stmt.step () == Sqlite.ROW) {
    string val = stmt.getString(stmt.getColumnIndexOrThrow("nombre"));
    stdout.printf ("servicio: %s\n", val);
}
    
answered by 26.02.2018 в 18:40