Skip to content

Instantly share code, notes, and snippets.

@rtravis
Last active December 10, 2015 06:50
Show Gist options
  • Save rtravis/cd32113c0fc543da120d to your computer and use it in GitHub Desktop.
Save rtravis/cd32113c0fc543da120d to your computer and use it in GitHub Desktop.
/*
* test-sparql1.c - run SPARQL query against a librdf.sqlite model
*
* created for librdf.sqlite issue #17, https://github.com/mro/librdf.sqlite/issues/17
*
* compile:
* gcc -I/usr/include/raptor2 -I/usr/include/rasqal -lrdf -lsqlite3 -o "test-sparql1" "test-sparql1.c"
*/
#include <stdio.h>
#include "rdf_storage_sqlite_mro.c"
static const char file_path[] = "books_sample.sqlite";
static const char query_string[] = ""
"SELECT ?author ?title\n"
"WHERE {\n"
" ?book a <http://dbpedia.org/ontology/Book> .\n"
" ?book <http://xmlns.com/foaf/0.1/name> ?title .\n"
" ?book <http://dbpedia.org/ontology/author> ?author.\n"
"}\n";
int main()
{
// for brevity error handling is removed
librdf_world *world = librdf_new_world();
// register SQLite storage factory
librdf_init_storage_sqlite_mro(world);
const char *options = "new='no',contexts='yes',synchronous='off'";
librdf_storage *store = librdf_new_storage(world,
LIBRDF_STORAGE_SQLITE_MRO,
file_path,
options);
librdf_model *model = librdf_new_model(world, store, NULL);
librdf_query *rdf_query = librdf_new_query(world, "sparql", NULL,
(const unsigned char*) query_string,
NULL);
librdf_query_results *res = librdf_query_execute(rdf_query, model);
if (res) {
// display query results to console in CSV format
librdf_query_results_to_file_handle2(res, stdout, "csv",
NULL, NULL, NULL);
}
librdf_free_query_results(res);
librdf_free_query(rdf_query);
librdf_free_model(model);
librdf_free_storage(store);
librdf_free_world(world);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment