[Vala] SQLite segfault when used in a class



Hi all,

I am trying to use SQLite. I followed the site tutorial.

BTW, there is an error in the example:

rc = Database.open(args[1], ref db);
   if ( rc == 0 ) { // SQLITE_OK -> Sqlite.OK == 0

it should be:

   if ( rc != 0 ) { // SQLITE_OK -> Sqlite.OK == 0


That corrected, I compiled the demo flawlessly, but when I try to create
a class to manage my DB I get a segfault on the callback:

public class Storage : GLib.Object
{
  private Sqlite.Database db;

  public void test()
  {
    string errmsg;
    int rc;

    rc = Sqlite.Database.open("test.sq3", ref this.db);
    if (rc != 0) {
      GLib.stdout.printf("DB error: %s\n", this.db.errmsg);
    }

    rc = this.db.exec("select * from myt;", callback, null, ref errmsg);

    if (rc != 0) {
      stderr.printf("SQL error: %s\n", errmsg);
    }
  }
        
  public static int callback(pointer data, int n_columns, string[]
values, string[] column_names)
  {
    for (int i = 0; i < n_columns; i++) {
        // HERE it segfaults:
      GLib.stdout.printf("%s = %s\n", column_names[i], values[i]);
    }
    GLib.stdout.printf("\n");

    return 0;
  }
}

public class Main : GLib.Object
{
  static int main (string[] args) {
        
    var db = new Storage();
    db.test();
  }
}

It seems that the parameters get "shifted left by one". I tried printing
the values, and I found that values[0] contains the column name of the
first column, while column_name[0] points to nowhere, giving the
segfault.

Am I doing something wrong?

Thank you very much.


-- 
Alessandro Pellizzari





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]