File: connection-example.c

package info (click to toggle)
tinysparql 3.10.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,468 kB
  • sloc: ansic: 118,310; python: 6,139; javascript: 719; sh: 121; perl: 106; xml: 67; makefile: 31; sql: 1
file content (60 lines) | stat: -rw-r--r-- 1,915 bytes parent folder | download | duplicates (2)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <tinysparql.h>

#define REMOTE_NAME "org.freedesktop.Tracker3.Miner.Files"

int main (int argc, const char **argv)
{
  g_autoptr (GError) error = NULL;
  g_autoptr (TrackerSparqlConnection) connection = NULL;
  g_autoptr (TrackerSparqlCursor) cursor = NULL;
  g_autoptr (TrackerSparqlStatement) stmt = NULL;
  const char *query = "SELECT nie:url(?u) WHERE { ?u a nfo:FileDataObject ; nfo:fileName ~name }";
  const char *name = NULL;
  int i = 0;

  connection = tracker_sparql_connection_bus_new (REMOTE_NAME, NULL, NULL, &error);
  if (!connection) {
    g_printerr ("Couldn't obtain a connection to the Tracker store: %s",
                error ? error->message : "unknown error");
    return 1;
  }

  /* Create a prepared statement */
  stmt = tracker_sparql_connection_query_statement (connection,
                                                    query,
                                                    NULL,
                                                    &error);

  if (!stmt) {
    g_printerr ("Couldn't create a prepared statement: '%s'",
                error->message);
    return 1;
  }

  /* Bind a value to the query variables */
  name = argv[1] ? argv[1] : "";
  tracker_sparql_statement_bind_string (stmt, "name", name);

  /* Executes the SPARQL query with the currently bound values and get new cursor */
  cursor = tracker_sparql_statement_execute (stmt, NULL, &error);
  if (!cursor) {
    g_printerr ("Couldn't execute query: '%s'",
                error->message);
    return 1;
  }

  /* Iterate synchronously the results */
  while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
    g_print ("Result [%d]: %s\n",
             i++,
             tracker_sparql_cursor_get_string (cursor, 0, NULL));
  }

  g_print ("A total of '%d' results were found\n", i);

  tracker_sparql_cursor_close (cursor);

  tracker_sparql_connection_close (connection);

  return 0;
}