SPARQL 参数化查询

发布于 2024-12-19 23:58:11 字数 1025 浏览 2 评论 0原文

再会!我将 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 技术交流群。

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

发布评论

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

评论(1

顾忌 2024-12-26 23:58:11

如果您指的是查询中的 Python 变量,您可以这样做...

qres = g.query(
    """SELECT ?x ?z ?y
        WHERE {
           """+some_uri+""" course:termName ?x.
           """+some_uri+""" ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
        }"""
        ,initNs=dict(course=Namespace.....

如果您想将 course:OSPF 转换为 SPARQL 中的变量,那么...

qres = g.query(
    """SELECT ?newVar ?x ?z ?y
        WHERE {
           ?newVar course:termName ?x.
           ?newVar ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
        }"""
        ,initNs=dict(course=Namespace.....

如果您详细解释一下查询的作用以及数据的外观,那么我们也许能够提供更好的帮助。

已编辑

您可能想要做的唯一更改是在不重复变量的情况下制定 SPARQL 查询,例如...

q = """SELECT ?x ?z ?y
        WHERE {
           course:%s course:termName ?x;
                  ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,'[^a-z0-9]','i') && isLiteral(?z) )
        }ORDER BY ASC(?s)
        """%var_value

注意第一个三元组模式末尾的 ; 。我不太理解 ?s ?d ?z 模式,我需要查看一些示例数据。我怀疑您试图通过此查询实现太多目标。如果你的数据集很大,这个查询将会非常慢。如果没有看到数据,我无法说更多。

If you mean a Python variable in the query you could do just ...

qres = g.query(
    """SELECT ?x ?z ?y
        WHERE {
           """+some_uri+""" course:termName ?x.
           """+some_uri+""" ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
        }"""
        ,initNs=dict(course=Namespace.....

If you want to transform course:OSPF into a variable in SPARQL then ...

qres = g.query(
    """SELECT ?newVar ?x ?z ?y
        WHERE {
           ?newVar course:termName ?x.
           ?newVar ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,"[^a-z]","i") && isLiteral(?z) )
        }"""
        ,initNs=dict(course=Namespace.....

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 ...

q = """SELECT ?x ?z ?y
        WHERE {
           course:%s course:termName ?x;
                  ?s ?t.
           ?s ?d ?z.
           ?t course:termName ?y.
           FILTER (regex(?z,'[^a-z0-9]','i') && isLiteral(?z) )
        }ORDER BY ASC(?s)
        """%var_value

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.

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