UPDATE queries
SPARQL UPDATE queries modify data in the triplestore. sparqlite supports all SPARQL 1.1 Update operations through the update() method.
Basic UPDATE query
Section titled “Basic UPDATE query”from sparqlite import SPARQLClient
with SPARQLClient("https://your-triplestore.example/sparql") as client: client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
INSERT DATA { <https://w3id.org/oc/meta/br/1> a fabio:JournalArticle ; dcterms:title "A study on citation networks" . } """)INSERT DATA
Section titled “INSERT DATA”Add new triples to the graph:
with SPARQLClient("https://your-triplestore.example/sparql") as client: client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX prism: <http://prismstandard.org/namespaces/basic/2.0/> PREFIX pro: <http://purl.org/spar/pro/>
INSERT DATA { <https://w3id.org/oc/meta/br/2> a fabio:JournalArticle ; dcterms:title "Machine learning in bibliometrics" ; prism:publicationDate "2024-01-15" ; pro:isDocumentContextFor <https://w3id.org/oc/meta/ar/1> . } """)DELETE DATA
Section titled “DELETE DATA”Remove specific triples:
with SPARQLClient("https://your-triplestore.example/sparql") as client: client.update(""" PREFIX dcterms: <http://purl.org/dc/terms/>
DELETE DATA { <https://w3id.org/oc/meta/br/2> dcterms:title "Machine learning in bibliometrics" . } """)DELETE/INSERT with WHERE
Section titled “DELETE/INSERT with WHERE”Modify existing data based on patterns:
with SPARQLClient("https://your-triplestore.example/sparql") as client: # Update an article title client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
DELETE { ?article dcterms:title ?oldTitle } INSERT { ?article dcterms:title "Machine learning in bibliometrics (revised)" } WHERE { ?article a fabio:JournalArticle ; dcterms:title ?oldTitle . FILTER(?oldTitle = "Machine learning in bibliometrics") } """)DELETE WHERE
Section titled “DELETE WHERE”Delete triples matching a pattern:
with SPARQLClient("https://your-triplestore.example/sparql") as client: # Delete all triples about a specific article client.update(""" DELETE WHERE { <https://w3id.org/oc/meta/br/2> ?p ?o . } """)CLEAR and DROP
Section titled “CLEAR and DROP”Remove all data from a graph:
with SPARQLClient("https://your-triplestore.example/sparql") as client: # Clear a named graph containing citation data client.update("CLEAR GRAPH <https://w3id.org/oc/meta/citations>")
# Clear the default graph client.update("CLEAR DEFAULT")
# Drop a named graph (removes the graph entirely) client.update("DROP GRAPH <https://w3id.org/oc/meta/citations>")Working with named graphs
Section titled “Working with named graphs”with SPARQLClient("https://your-triplestore.example/sparql") as client: # Insert into a specific graph client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
INSERT DATA { GRAPH <https://w3id.org/oc/meta/articles> { <https://w3id.org/oc/meta/br/3> a fabio:JournalArticle ; dcterms:title "Semantic web technologies for open science" . } } """)
# Copy between graphs client.update("COPY <https://w3id.org/oc/meta/articles> TO <https://w3id.org/oc/meta/backup>")
# Move a graph client.update("MOVE <https://w3id.org/oc/meta/staging> TO <https://w3id.org/oc/meta/articles>")Return value
Section titled “Return value”The update() method returns None. It does not return any data:
with SPARQLClient("https://your-triplestore.example/sparql") as client: result = client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
INSERT DATA { <https://w3id.org/oc/meta/br/4> a fabio:JournalArticle ; dcterms:title "Open access publishing trends" . } """) print(result) # NoneTo verify an update, execute a SELECT or ASK query afterward:
with SPARQLClient("https://your-triplestore.example/sparql") as client: client.update(""" PREFIX fabio: <http://purl.org/spar/fabio/> PREFIX dcterms: <http://purl.org/dc/terms/>
INSERT DATA { <https://w3id.org/oc/meta/br/4> a fabio:JournalArticle ; dcterms:title "Open access publishing trends" . } """)
exists = client.ask(""" PREFIX fabio: <http://purl.org/spar/fabio/>
ASK { <https://w3id.org/oc/meta/br/4> a fabio:JournalArticle . } """) if exists: print("Article inserted successfully")