summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2021-04-25 12:18:22 +1000
committerNicholas Car <nicholas.car@surroundaustralia.com>2021-04-25 12:18:22 +1000
commitc6b308969f312f35d6e31ba404d38aef7d534db2 (patch)
tree2690eeb1f0efe9f04af431898b5ca6d24f0c207e /docs
parent4164d8319a0685b461b00ac282657b63684fbc64 (diff)
downloadrdflib-c6b308969f312f35d6e31ba404d38aef7d534db2.tar.gz
added UPDATE example
Diffstat (limited to 'docs')
-rw-r--r--docs/intro_to_sparql.rst63
1 files changed, 61 insertions, 2 deletions
diff --git a/docs/intro_to_sparql.rst b/docs/intro_to_sparql.rst
index e8856bb7..3ad00c2c 100644
--- a/docs/intro_to_sparql.rst
+++ b/docs/intro_to_sparql.rst
@@ -66,8 +66,67 @@ Variables can also be pre-bound, using the ``initBindings`` kwarg which can
pass in a ``dict`` of initial bindings. This is particularly
useful for prepared queries, as described below.
-Quering a Remote Service
-^^^^^^^^^^^^^^^^^^^^^^^^
+Update Queries
+^^^^^^^^^^^^^^
+
+Update queries are performed just like reading queries but using the :meth:`rdflib.graph.Graph.update` method. An
+example:
+
+.. code-block:: python
+
+ from rdflib import Graph
+
+ # Create a Graph, add in some test data
+ g = Graph()
+ g.parse(
+ data="""
+ <x:> a <c:> .
+ <y:> a <c:> .
+ """,
+ format="turtle"
+ )
+
+ # Select all the things (s) that are of type (rdf:type) c:
+ qres = g.query("""SELECT ?s WHERE { ?s a <c:> }""")
+
+ for row in qres:
+ print(f"{row.s}")
+ # prints:
+ # x:
+ # y:
+
+ # Add in a new triple using SPATQL UPDATE
+ g.update("""INSERT DATA { <z:> a <c:> }""")
+
+ # Select all the things (s) that are of type (rdf:type) c:
+ qres = g.query("""SELECT ?s WHERE { ?s a <c:> }""")
+
+ print("After update:")
+ for row in qres:
+ print(f"{row.s}")
+ # prints:
+ # x:
+ # y:
+ # z:
+
+ # Change type of <y:> from <c:> to <d:>
+ g.update("""
+ DELETE { <y:> a <c:> }
+ INSERT { <y:> a <d:> }
+ WHERE { <y:> a <c:> }
+ """)
+ print("After second update:")
+ qres = g.query("""SELECT ?s ?o WHERE { ?s a ?o }""")
+ for row in qres:
+ print(f"{row.s} a {row.o}")
+ # prints:
+ # x: a c:
+ # z: a c:
+ # y: a d:
+
+
+Querying a Remote Service
+^^^^^^^^^^^^^^^^^^^^^^^^^
The ``SERVICE`` keyword of SPARQL 1.1 can send a query to a remote SPARQL endpoint.