Sparql Parameterized Queries
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
Solution 1:
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.
Post a Comment for "Sparql Parameterized Queries"