SPARQL 参数化查询
再会!我将 rdflib 用于 python。我有一个问题。如何将变量放入 SPARQL 的查询中? 当然不是“OSPF”:OSPF!
qres = g.query(
"""SELECT ?x ?z ?y
WHERE {
course:OSPF course:termName ?x.
course:OSPF ?s ?t.
?s ?d ?z.
?t course:termName ?y.
FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
}"""
,initNs=dict(course=Namespace.....
@msalvadores 我想通过控制台输入我的变量。 --->python parse.py OSPF 变量(OSPF)的值可能是另一个值。如何将其初始化为查询(WHERE)?几天前我已经通过变量插值解决了我的问题。像这样:
qtest = "OSPF","OSPF"
q =( """SELECT ?x ?z ?y\
WHERE {\
course:%s course:termName ?x.\
course:%s ?s ?t.\
?s ?d ?z.\
?t course:termName ?y.\
FILTER (regex(?z,'[^a-z0-9]','i') && isLiteral(?z) )\
}ORDER BY ASC(?s)\
""")% qtest
qres = g.query(q, initNs=dict(course=Namespace
但我想可以用另一种方式来完成。因为在我看来,我提出的解决方案并不完全正确。
Good day! I apply rdflib for python. I have a question. How can I put variable into SPARQL's query ?
Instead of 'OSPF' in course:OSPF!
qres = g.query(
"""SELECT ?x ?z ?y
WHERE {
course:OSPF course:termName ?x.
course:OSPF ?s ?t.
?s ?d ?z.
?t course:termName ?y.
FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
}"""
,initNs=dict(course=Namespace.....
@msalvadores
I want enter my Variable by console. --->python parse.py OSPF A value of variable(OSPF) may be another one. How can I initialize it into query(WHERE)? I have resolved my question by interpolation of variable several days ago. Like this:
qtest = "OSPF","OSPF"
q =( """SELECT ?x ?z ?y\
WHERE {\
course:%s course:termName ?x.\
course:%s ?s ?t.\
?s ?d ?z.\
?t course:termName ?y.\
FILTER (regex(?z,'[^a-z0-9]','i') && isLiteral(?z) )\
}ORDER BY ASC(?s)\
""")% qtest
qres = g.query(q, initNs=dict(course=Namespace
But I suppose it could be done another way. Because on my opinion the solution is not quite right presented by me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您指的是查询中的 Python 变量,您可以这样做...
如果您想将 course:OSPF 转换为 SPARQL 中的变量,那么...
如果您详细解释一下查询的作用以及数据的外观,那么我们也许能够提供更好的帮助。
已编辑
您可能想要做的唯一更改是在不重复变量的情况下制定 SPARQL 查询,例如...
注意第一个三元组模式末尾的
;
。我不太理解?s ?d ?z
模式,我需要查看一些示例数据。我怀疑您试图通过此查询实现太多目标。如果你的数据集很大,这个查询将会非常慢。如果没有看到数据,我无法说更多。If you mean a Python variable in the query you could do just ...
If you want to transform course:OSPF into a variable in SPARQL then ...
If you explain a bit more what your query does and how your data looks like then we might be able to help better.
Edited
Only change that yo might want to do is to formulate the SPARQL query without repeating the variable, something like ...
Notice the
;
at the end of the first triple pattern. I do not really understand the?s ?d ?z
pattern, I need to see some sample data. I suspect that you are trying to achieve too much with this query. If your dataset is big this query is going to be very slow. I cannot say more than this without seeing the data.