如何解决无法使用python SPARQLWrapper从GraphDB中删除任何三元组
我正在尝试使用python的SPARQLWrapper和我在此处找到的代码段从GraphDB中删除一个简单的三元组(版本= GraphDB free):https://github.com/RDFLib/sparqlwrapper-更新示例。我总是得到以下异常: SPARQLWrapper.SPARQLExceptions.QueryBadFormed:QueryBadFormed:错误的请求已发送到端点,可能是sparql查询的格式错误。
我的代码是:
sparql = SPARQLWrapper('http://192.168.0.242:7200/repositories/DataCitation')
sparql.setMethod(POST)
sparql.setQuery("""
PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
delete where {
<http://ontology.ontotext.com/resource/tsk9hdnas934> pub:occupation "Cook".
}
""")
results = sparql.query()
print(results.response.read())
当我向相同的端点执行“询问”或“选择”语句时,我得到一个有效的结果。仅更新语句不起作用。
这是完整的堆栈跟踪
/home/filip/anaconda3/envs/TripleStoreCitationFramework/bin/python /home/filip/PycharmProjects/TripleStoreCitationFramework/GraphDB/Playground.py
Traceback (most recent call last):
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py",line 1073,in _query
response = urlopener(request)
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 222,in urlopen
return opener.open(url,data,timeout)
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 531,in open
response = meth(req,response)
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 640,in http_response
response = self.parent.error(
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 569,in error
return self._call_chain(*args)
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 502,in _call_chain
result = func(*args)
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/urllib/request.py",line 649,in http_error_default
raise HTTPError(req.full_url,code,msg,hdrs,fp)
urllib.error.HTTPError: HTTP Error 400:
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "/home/filip/PycharmProjects/TripleStoreCitationFramework/GraphDB/Playground.py",line 6,in <module>
citing.delete_triples("s")
File "/home/filip/PycharmProjects/TripleStoreCitationFramework/GraphDB/DataCiting.py",line 32,in delete_triples
results = sparql.query()
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py",line 1107,in query
return QueryResult(self._query())
File "/home/filip/anaconda3/envs/TripleStoreCitationFramework/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py",line 1077,in _query
raise QueryBadFormed(e.read())
SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint,probably the sparql query is bad formed.
响应: b'缺少参数:查询'
解决方法
您需要插入语句的端点不是用于普通查询的简单SPARQL端点,而是专用的/ statements端点:
http:/// repositories // statements
此端点还用于DELETE语句。
您可以在RDF4J文档中查找一些示例。 此外,如果要通过查询字符串传递数据,而不是将其作为请求正文的一部分,则需要意识到以下事实:该数据必须以“?update =“开头,而不是“?query =”开头。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。