【问题标题】:Cannot delete any triple from GraphDB using pythons SPARQLWrapper无法使用 pythons SPARQLWrapper 从 GraphDB 中删除任何三元组
【发布时间】:2020-08-31 00:52:04
【问题描述】:

我正在尝试使用 python 的 SPARQLWrapper 和我在此处找到的代码 sn-p 从 GraphDB(版本 = GraphDB 免费)中删除一个简单的三元组: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'缺少参数:查询'

【问题讨论】:

标签: python sparql graphdb sparqlwrapper


【解决方案1】:

您需要插入语句的端点不是用于普通查询的简单 SPARQL 端点,而是专用的 /statements 端点:

http:///repositories//语句

此端点也用于 DELETE 语句。

您可以在 RDF4J 文档中查找一些示例。 此外,如果您使用查询字符串而不是将其作为请求正文的一部分传递数据,则需要注意它必须以“?update=”而不是“?query=”开头。呵呵

【讨论】:

  • 换句话说,URL 应该是http://192.168.0.242:7200/repositories/DataCitation/statements?update=...。如果你使用 POST,你应该使用带有参数update 的 www-form-url-encoded 并且值是查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 2021-01-20
  • 2018-01-29
相关资源
最近更新 更多