SPARQL插入异常:SPARQLWrapper.SPARQLExceptions.QueryBadFormed

2022-03-26 00:00:00 python sparql semantic-web sparqlwrapper

问题描述

我一直有这个奇怪的问题,我试图使用python中的SPARQLWrapper库将其插入到Virtuoso图形中。 我可以通过基于浏览器的端点在localhost:8890sparql插入一个三元组,但是当我通过我的python SparqlWrapper尝试相同的查询时,它抛出以下错误:

SPARQLWrapper.SPARQLExceptions.QueryBadFormed:查询错误格式:A错误 请求已发送到端点,SPARQL查询可能是 格式不正确。

我觉得配置端有问题,但无法识别。

PREFIX dbpedia: <http://dbpedia.org/resource/> Insert Data  { GRAPH <test> { <http://dbpedia.org/resource/life> <http://umbel.org/umbel/rc/Artist> '2' . } }
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    sys.exit(process.run("1"))
  File "test.py", line 27, in run
    result = self.sparql.query().convert()
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SPARQLWrapper/Wrapper.py", line 390, in query
    return QueryResult(self._query())
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SPARQLWrapper/Wrapper.py", line 363, in _query
    raise QueryBadFormed()
SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.

解决方案

如果您将其带到parql.org的SPARQL update validator,它会立即告诉您这是错误的,语法错误在哪里:

输入:

  1 PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>
  2 PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
  3 PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
  4 PREFIX owl:     <http://www.w3.org/2002/07/owl#>
  5 PREFIX fn:      <http://www.w3.org/2005/xpath-functions#>
  6 PREFIX apf:     <http://jena.hpl.hp.com/ARQ/property#>
  7 
  8 PREFIX dbpedia: <http://dbpedia.org/resource/>
  9 Insert Data Into GRAPH <test> {
 10   <http://dbpedia.org/resource/life> <http://umbel.org/umbel/rc/Artist> '2'^^xsd:integer .
 11 }

语法错误:

Encountered " "into" "Into "" at line 9, column 13.
Was expecting:
    "{" ...
当然,要了解如何做您正在尝试做的事情,您需要查看规范。SPARQL 1.1 Update是要查看的地方,虽然我建议您浏览一下,以便更好地了解其中的内容,但一个示例就足以显示您需要编写的内容:

示例2:

此SPARQL 1.1更新请求添加了一个三元组,以提供 书。与上一个示例相反,该示例影响了默认 图形,则请求的更改发生在由标识的命名图形中 IRIhttp://example/bookStore

PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
INSERT DATA
{ GRAPH <http://example/bookStore> { <http://example/book1>  ns:price  42 } }

您的查询需要

PREFIX dbpedia: <http://dbpedia.org/resource/>
Insert Data 
{ GRAPH <test> { <http://dbpedia.org/resource/life> <http://umbel.org/umbel/rc/Artist> '2'^^xsd:integer . } }

相关文章