使用SPARQLWrapper进行INSERT / DELETE / UPDATE查询

时间:2013-01-04 15:56:59

标签: python sparql rdflib

虽然我在网上已经通过大量的例子来解释使用python SPARQLWrapper使用SELECT语句从sesame triple store中获取数据,但不知道我们如何使用它来在芝麻中INSERT / DELETE / UPDATE语句。你们中的任何人都可以在这方面提供指导。

由于

2 个答案:

答案 0 :(得分:7)

SPARQL查询作为GET请求发送,但UPDATE(如INSERT,DELETE等)要求将查询作为POST请求发送。只需在sparql.query()

之前添加以下行
sparql.method = 'POST'

此外,更新的URL与查询不同。更新基于工作台而不是芝麻网址。例如,如果查询网址为:

http://localhost:8080/openrdf-sesame/repositories/test/

http://localhost:8080/openrdf-workbench/repositories/test/query

然后更新网址将是:

http://localhost:8080/openrdf-workbench/repositories/test/update

因此,UPDATE / INSERT请求应如下所示:

queryString = "INSERT DATA { GRAPH <http://example.com/> { "b" a "c". } }" 
sparql = SPARQLWrapper("http://localhost:8080/openrdf-workbench/repositories/test/update")

sparql.setQuery(queryString) 
sparql.method = 'POST'
sparql.query()

答案 1 :(得分:1)

这在文档中并不是特别清楚,但我认为你可以像执行查询一样执行更新语句:

queryString = "DELETE WHERE { ?s ?p ?o. }" 
sparql = SPARQLWrapper("http://localhost:8080/openrdf-sesame/repositories/test/statements")

sparql.setQuery(queryString) 
ret = sparql.query()

对于芝麻,需要记住的一点是,更新端点(repositories/<repId>/statements)的URL与查询端点(repositories/<repId>)的URL不同。有关详细信息,请参阅Sesame protocol docs

相关问题