Can't get INSERT to work in SPARQLWrapper

别说谁变了你拦得住时间么 提交于 2020-01-04 06:58:14

问题


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.


回答1:


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



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()


来源:https://stackoverflow.com/questions/46271187/cant-get-insert-to-work-in-sparqlwrapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!