使用Python rdflib的参数化SPARQL查询
我正在尝试使用rdflib的参数化SPARQL查询查询一个非常简单的数据库:
x = "Inconel_625"
g = Graph()
g.parse ("Desktop/materials.ttl")
knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label """+x+""";
mydb:min ?min ;
mydb:max ?max.
}"""
qres = g.query(knows_query)
for x in qres:
print(x.min, x.max)
不幸的是,此代码不起作用。如果我用“ Inconel_625替换”“”+X+“”。它可以以某种方式起作用。 我没有看到什么?是否不可能在rdflib中参数化SPARQL查询?
I am trying to query a very simple database about Alloys with a parametrized SPARQL query using RDFLib:
x = "Inconel_625"
g = Graph()
g.parse ("Desktop/materials.ttl")
knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label """+x+""";
mydb:min ?min ;
mydb:max ?max.
}"""
qres = g.query(knows_query)
for x in qres:
print(x.min, x.max)
Unfortunatly this code does not work. If i replace """+x+""" with "Inconel_625 it works somehow.
What am i not seeing? Is it not possible to parametrize a sparql query in RDFLib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,RDFLIB通过允许一个人通过
Initbindings
关键字参数绑定到变量来支持参数化查询,以graph.query()
。这是修改您的示例代码以使用它:
Yes, rdflib supports parameterized queries by allowing one to bind values to variables via the
initBindings
keyword argument toGraph.query()
.Here is your example code modified to use it:
属性RDF:标签期望在对象的位置有一个字符串。您没有将对象放在倒逗号中。您应该这样做:
现在,如果您要打印此查询,它将在控制台上显示以下输出:
哪个是您想要的,它应该起作用。让我知道您是否仍然无法正常工作。我很乐意为您提供帮助。
the property rdf:label expects a string at the place of object. You are not putting the object in inverted commas. You should do it like this:
Now if you will print this query, it will show the following output on the console:
Which is what you want and it should work. Let me know if you still don't get it to work. I will be happy to help.