Skip to main content
 首页 » 编程设计

python之无法让 INSERT 在 SPARQLWrapper 中工作

2025年02月15日25虾米哥

我一直在尝试让 SPARQLWrapper 将一个简单的三元组插入到 GraphDB 中,但没有成功。我对选择查询没有问题。这是失败的 Python 测试代码:

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

它返回这些错误:

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

和

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

回应: b'缺少参数:查询'

我到处都看过并尝试了所有建议,但无法正常工作。感谢任何好的线索。

请参阅我添加的关于查询验证的评论。问题重复且已回答的建议不适用。

请您参考如下方法:

GraphDB 公开 Sesame-style 端点 URL。
下面是 GraphDB 8.3 Workbench 帮助页面的屏幕截图(我使用的是免费版)。



以下 Python 代码适用于我(repositoryID 是 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()