7

Although I have gone through lot of examples on the web explaining the use of python SPARQLWrapper using SELECT statements for fetching data from sesame triple store, but not sure how can we INSERT/DELETE/UPDATE statements in sesame using it. Can any of you please guide in this regard.

Thanks

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
imran
  • 199
  • 3
  • 11

2 Answers2

11

The SPARQL queries are send as GET request, but the UPDATE (like INSERT, DELETE, etc.) requires the query be send as a POST request. Just add the following line before sparql.query()

sparql.method = 'POST'

Also, the url for update is different from the query. The update is based on workbench and not sesame url. For example, if the query url is:

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

or

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

then the update url would be:

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

Therefore, the UPDATE/INSERT request should look like:

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()
Khwaishien
  • 1,818
  • 4
  • 15
  • 14
2

This is not particularly clear from the docs, but I think you can just execute an update statement in the same way that you execute a query:

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

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

In the case of Sesame, one thing to keep in mind is that the URL for the update endpoint (repositories/<repId>/statements) is not the same as the URL for the query endpoint (repositories/<repId>). See the Sesame protocol docs for details.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • this URL gives error, SPARQLWrapper.SPARQLExceptions.EndPointInternalError: EndPointInternalError: endpoint returned code 500 and response. org.openrdf.workbench.exceptions.BadRequestException: Unconfigured path: /statements. I have tried lot of other URL e.g. (http://localhost:8080/openrdf-sesame/repositories/test/remove") but no success. Thanks for answering. – imran Jan 07 '13 at 18:29
  • 1
    Did you replace the `test` bit in the url with the actual id of your repository? I've added a link to the Sesame protocol docs to the answer, which gives more details. – Jeen Broekstra Jan 07 '13 at 22:41
  • 1
    Also: a *Workbench* exception? The Workbench shouldn't play any part in this setup at all.. – Jeen Broekstra Jan 07 '13 at 22:42
  • Ah! I was doing mistake in . Now my URL is http://localhost:8080/openrdf-sesame/repositories/sample/statements and when I run it gives following error: urllib2.HTTPError: HTTP Error 406: Not Acceptable. I have also visited sesame protocol docs and trying to understand the reason behind this error. I am extremely thankful for your help. – imran Jan 08 '13 at 14:31
  • 1
    Sesame sends a HTTP 406 error when the client does not specify an acceptable response format. So this looks like a problem in the SPARQLWrapper. If I were you I'd get in touch with the developers directly (I believe they have a mailinglist). – Jeen Broekstra Jan 08 '13 at 21:49