pythonsparqlgraphdbsparqlwrapper

Can't get INSERT to work in SPARQLWrapper


I've been trying to get SPARQLWrapper to insert a simple triple into GraphDB with no success. I have no trouble with select queries. Here's Python test code that fails:

db = sparqlw.SPARQLWrapper(endpoint)
query = '''
INSERT {<http://example.com/123456789876> a owl:Thing .}
WHERE {}
'''
db.setQuery(query)
db.method = sparqlw.POST
db.setReturnFormat(sparqlw.JSON)
db.queryType= sparqlw.INSERT
result = db.query()

It returns these errors:

"urllib.error.HTTPError: HTTP Error 400: Bad Request"

and

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

Response: b'Missing parameter: query'

I've looked everywhere and tried everything suggested and can't get it to work. Grateful for any good leads.

See my added comment on validation of the query. The suggestion that the question is a duplicate and already answered is not applicable.


Solution

  • GraphDB exposes Sesame-style endpoint URLs.
    Here below a screenshot of GraphDB 8.3 Workbench help page (I'm using Free Edition).


    Help


    The following Python code works for me (repositoryID is wikidata):

    from SPARQLWrapper import SPARQLWrapper, BASIC
    
    db = SPARQLWrapper("http://localhost:7200/repositories/wikidata/statements")
    query = '''
    INSERT {<http://example.com/123456789879> a owl:Thing .}
    WHERE {}
    '''
    db.setHTTPAuth(BASIC)
    db.setCredentials('login', 'password')
    db.setQuery(query)
    db.method = "POST"
    db.setReturnFormat('json')
    db.queryType = "INSERT"
    result = db.query()