使用Python rdflib的参数化SPARQL查询

发布于 2025-02-07 17:31:37 字数 407 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2025-02-14 17:31:38

是的,RDFLIB通过允许一个人通过Initbindings关键字参数绑定到变量来支持参数化查询,以 graph.query()

这是修改您的示例代码以使用它:

from rdflib import Graph, Literal

label = "Inconel_625"

g = Graph()
g.parse("Desktop/materials.ttl")

knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label ?label ;
 mydb:min ?min ;
 mydb:max ?max .
}"""

qres = g.query(knows_query, initBindings={"label": Literal(label)})
for x in qres:
    print(x.min, x.max)

Yes, rdflib supports parameterized queries by allowing one to bind values to variables via the initBindings keyword argument to Graph.query().

Here is your example code modified to use it:

from rdflib import Graph, Literal

label = "Inconel_625"

g = Graph()
g.parse("Desktop/materials.ttl")

knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label ?label ;
 mydb:min ?min ;
 mydb:max ?max .
}"""

qres = g.query(knows_query, initBindings={"label": Literal(label)})
for x in qres:
    print(x.min, x.max)
给我一枪 2025-02-14 17:31:38

属性RDF:标签期望在对象的位置有一个字符串。您没有将对象放在倒逗号中。您应该这样做:

knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label  \""""+x+"""\";
 mydb:min ?min ;
 mydb:max ?max.
}"""

现在,如果您要打印此查询,它将在控制台上显示以下输出:

SELECT ?min ?max
WHERE
{ ?s rdfs:label  "Inconel_625";
 mydb:min ?min ;
 mydb:max ?max.
}

哪个是您想要的,它应该起作用。让我知道您是否仍然无法正常工作。我很乐意为您提供帮助。

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:

knows_query = """
SELECT ?min ?max
WHERE
{ ?s rdfs:label  \""""+x+"""\";
 mydb:min ?min ;
 mydb:max ?max.
}"""

Now if you will print this query, it will show the following output on the console:

SELECT ?min ?max
WHERE
{ ?s rdfs:label  "Inconel_625";
 mydb:min ?min ;
 mydb:max ?max.
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文