Skip to content

sparql_update_example

SPARQL Update statements can be applied with Graph.update

Attributes:

EXAMPLES_DIR module-attribute

EXAMPLES_DIR = parent

g module-attribute

g = Graph()

lse show_docstring_attributes: false show_docstring_functions: false show_docstring_modules: false show_docstring_classes: false show_signature: false show_signature_annotations: false show_signature_type_parameters: false show_docstring_other_parameters: false show_docstring_parameters: false show_docstring_raises: false show_docstring_receives: false show_docstring_returns: false summary: false show_if_no_docstring: false

from pathlib import Path

import rdflib

EXAMPLES_DIR = Path(__file__).parent


if __name__ == "__main__":
    g = rdflib.Graph()
    g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}", format="n3")

    print(f"Initially there are {len(g)} triples in the graph")

    g.update(
        """
        PREFIX foaf: <http://xmlns.com/foaf/0.1/>
        PREFIX dbpedia: <http://dbpedia.org/resource/>
        INSERT {
            ?s a dbpedia:Human .
        }
        WHERE {
            ?s a foaf:Person .
        }
        """
    )

    print(f"After the UPDATE, there are {len(g)} triples in the graph")