无法使用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 =”开头。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...