1

I have been having this weird issue, I am trying to insert into a virtuoso graph using SPARQLWrapper library in python. I am able to insert a triple via the browser based endpoint at localhost:8890\sparql , but when I try the same query via my python SparqlWrapper it throws the below error:

SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.

I feel there is something wrong at configuration end, but failed to identify the same.

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.
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Ankit Solanki
  • 670
  • 2
  • 9
  • 23
  • Is there a query, or are you using an empty string? ;) Please show your SPARQL query. There are query validators at http://sparql.org that you can use to check whether your query is well-formed, by the way. But please include the query, since "Questions concerning problems with code you've written must **describe the specific problem — and include valid code to reproduce it** — in the question itself." – Joshua Taylor Dec 17 '13 at 14:44
  • Apologies, Added the query! – Ankit Solanki Dec 17 '13 at 14:53

2 Answers2

3

If you take this to the sparql.org's SPARQL update validator, it will tell you immediately that this is wrong, and where the syntax error is:

Input:

  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 }

Syntax error:

Encountered " "into" "Into "" at line 9, column 13.
Was expecting:
    "{" ...

Of course, to find out how to do what you're trying to do, you'll need to look to the spec. The SPARQL 1.1 Update is the place to look, and while I'd advise you to skim through that to get a good idea of what's in there, one example will be enough to show what you need to write:

Example 2:

This SPARQL 1.1 Update request adds a triple to provide the price of a book. As opposed to the previous example, which affected the default graph, the requested change happens in the named graph identified by the IRI http://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 } }

Your query needs to be

PREFIX dbpedia: <http://dbpedia.org/resource/>
Insert Data 
{ GRAPH <test> { <http://dbpedia.org/resource/life> <http://umbel.org/umbel/rc/Artist> '2'^^xsd:integer . } }
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Josh Some how still the same error, also i tried the same code with two versions of virtuoso 6 and 7 respectively, much to my surprise my old code is working fine with version 7.x but throws the above error with 6.x – Ankit Solanki Dec 17 '13 at 16:09
  • PREFIX dbpedia: Insert Data { GRAPH { '2' . }} Traceback (most recent call last): File "test.py", line 37, in sys.exit(process.run("1")) File "test.py", line 31, 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 – Ankit Solanki Dec 17 '13 at 16:10
  • 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. – Ankit Solanki Dec 17 '13 at 16:10
  • @AnkitSolanki It's next to impossible to read code in comments. Please post this as an update to your question. Also, I very much doubt that it's the _same_ error. I see, for instance, `PREFIX dbpedia: ;` with a final semicolon (which was _not_ in the query that I posted) that doesn't belong there. You may be getting the same _local_ error, but it's because of a different syntax error. – Joshua Taylor Dec 17 '13 at 16:13
  • @AnkitSolanki And that semicolon isn't the only one; I see that other sneaked in in other places, and your datatype on `'2'` is gone. **Please** post code as updates. Also, please validate your queries with sparql.org first. – Joshua Taylor Dec 17 '13 at 16:15
  • @AnkitSolanki That said, I _did_ have a missing brace in my answer (which I've now fixed). Either of us could have spotted that by using the query validator. – Joshua Taylor Dec 17 '13 at 16:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43351/discussion-between-ankit-solanki-and-joshua-taylor) – Ankit Solanki Dec 17 '13 at 16:18
  • pinged you on chat, also i tried the same code with other packages its working fine! – Ankit Solanki Dec 17 '13 at 16:28
  • 1
    @AnkitSolanki As I said, there _was_ an error in the code that I posted, but I've updated it now. Do note that SPARQL Update is a part of a later SPARQL revision (1.1). It looks like SPARQL Update 1.1 support was added in Virtuoso 7 (see [this announcement](http://www.openlinksw.com/dataspace/doc/dav/wiki/Main/#2013-08-05: New VOS 7.0.0 Released)), so it's not surprising if it doesn't work in earlier versions. – Joshua Taylor Dec 17 '13 at 16:31
  • Yup this confirms the same too : http://sourceforge.net/mailarchive/forum.php?thread_name=CAKZ-WGfuh5gDywtC4vZR72km6noOB-ZRQmfwoOzHB6q%2BYAvUsA%40mail.gmail.com&forum_name=sparql-wrapper-devel – Ankit Solanki Dec 17 '13 at 16:36
0

I spent all day battling this problem. I finally found the solution to my version of this problem:

Make sure that your spaces are actually spaces and not just newlines "\n". The editors have no problem with this, SPARQLWrapper doesn't like it.

Wilmer E. Henao
  • 4,094
  • 2
  • 31
  • 39