Updating the Store: Tracker SPARQL Library Reference Manual

## Updating the Store

In order to perform updates in the store, a new TrackerSparqlConnection object must be acquired with `tracker_sparql_connection_get`. Once a proper connection object has been acquired, the update can be launched either synchronously (`tracker_sparql_connection_update`) or asynchronously (`tracker_sparql_connection_update_async`). If launched asynchronously, the result of the operation can be obtained with `tracker_sparql_connection_update_finish`.

Once you no longer need the connection, remember to call `g_object_unref` for the TrackerSparqlConnection.

The following program shows how a synchronous update can be done to the store:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    #include <libtracker-sparql/tracker-sparql.h>

    int main (int argc, const char **argv)
    {
      GError *error = NULL;
      TrackerSparqlConnection *connection;
      const gchar *query =
        "INSERT { "
        "  _:tag a nao:Tag ; "
        "        nao:prefLabel 'mylabel' . "
        "} WHERE { "
        "  OPTIONAL { "
        "    ?tag a nao:Tag ; "
        "    nao:prefLabel 'mylabel' "
        "  } . "
        "FILTER (!bound(?tag)) "
        "}";

      connection = tracker_sparql_connection_bus_new ("org.freedesktop.Tracker3.Miner.Files", NULL, NULL, &error);
      if (error) {
        g_printerr ("Couldn't obtain a connection to the Tracker store: %s",
          error->message);
        g_clear_error (&error);

        return 1;
      }

      /* Run a synchronous update query */
      tracker_sparql_connection_update (connection,
                              query,
                              NULL,
                              &error);
      if (error) {
        /* Some error happened performing the query, not good */
        g_printerr ("Couldn't update the Tracker store: %s",
          error->message);

        g_clear_error (&error);
        g_object_unref (connection);

        return 1;
      }

      g_object_unref (connection);

      return 0;
    }