Skip to content

Commit 7dc031f

Browse files
committed
added example of script that insert triples from a RDFLib Grapah using SPARQLUpdate INSERT query
1 parent 225cb70 commit 7dc031f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from rdflib import Graph, URIRef, Literal
2+
from rdflib.namespace import RDFS
3+
4+
g = Graph()
5+
asturias = URIRef("http://dbpedia.org/resource/Asturias")
6+
g.add( (asturias, RDFS.label, Literal('Asturies', lang="ast") ) )
7+
g.add( (asturias, RDFS.label, Literal('Asturias', lang="es") ) )
8+
g.add( (asturias, RDFS.label, Literal('Asturien', lang="de") ) )
9+
10+
###############################################################################
11+
triples = ""
12+
for subject,predicate,obj in g:
13+
triples = triples + subject.n3() + " " + predicate.n3() + " " + obj.n3() + " . \n"
14+
15+
query = """WITH <http://example.graph>
16+
INSERT {"""+ triples + """}"""
17+
###############################################################################
18+
19+
from SPARQLWrapper import SPARQLWrapper, POST, DIGEST
20+
21+
sparql = SPARQLWrapper("https://example.org/sparql-auth")
22+
sparql.setHTTPAuth(DIGEST)
23+
sparql.setCredentials("login", "password")
24+
sparql.setMethod(POST)
25+
sparql.setQuery(query)
26+
27+
results = sparql.query()
28+
print results.response.read()

0 commit comments

Comments
 (0)