Get the last inserted row

Libgda allows one to get the last inserted row right after an INSERT statement has been executed. Libgda returns a new GdaSet object (which the caller must unref when not needed anymore) which contains named values, one for each column of the table in which data has been inserted. To get that object, pass a place holder to the last_insert_row of gda_connection_statement_execute_non_select()

The following code example show how to use the returned GdaSet object:

GdaSet *last_row;
GdaStatement *stmt;
stmt = gda_sql_parser_parse_string (parser, "INSERT INTO mytable (name) VALUES ('joe')", NULL, NULL);
if (gda_connection_statement_execute_non_select (connection, stmt, NULL, &last_row, &error) == -1) {
    g_warning ("Can't execute INSERT: %s\n",
              error && error->message ? error->message : "???");
    if (error)
        g_error_free (error);
else {
    if (!last_row)
        g_print ("Last row not reported\n");
    else {
        GSList *list;
        for (list = last_row->holders; list; list = list->next) {
            GdaHolder *h = GDA_HOLDER (list->data);
            gchar *str;
            str = gda_value_stringify (gda_holder_get_value (h));
            g_print ("\t%s => %s\n", gda_holder_get_id (h), str);
            g_free (str);
    g_object_unref (last_row);
g_object_unref (stmt);

Which gives the following output (considering that in the example the "mytable" table has two columns: an Id and a Name columns):

        +0 => 1
        +1 => joe