INSERT/DELETE/UPDATE query using SPARQLWrapper

↘锁芯ラ 提交于 2019-12-04 04:02:16

问题


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


回答1:


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()



回答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.



来源:https://stackoverflow.com/questions/14160437/insert-delete-update-query-using-sparqlwrapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!