Using string replace isn't a great idea if you don't trust the input - re:
SQL/SPARQL injection and little bobby tables: https://xkcd.com/327/

I don't think it works with SPARQLWrapper - but with the SPARQLStore you
can pass your binding as RDFLib terms:

  dbo = Namespace("http://dbpedia.org/ontology/";)
  graph = Graph("SPARQLStore", identifier="http://dbpedia.org";)
  graph.open("http://dbpedia.org/sparql";)
  graph.bind('dbo', dbo)
  for row in graph.query('SELECT ?pop WHERE { ?city dbo:populationTotal
?pop }', initBindings={ 'city': URIRef("http://dbpedia.org/resource/Brisbane";)
})):
   .... do something with row ...

For mystery reasons this doesn't work with dbpedia though - it should
generate the query:
SELECT ?pop WHERE { ?city dbo:populationTotal ?pop } VALUES (?city) { ( <
http://dbpedia.org/resource/Brisbane>) }

which should work but doesn't. I don't have time to figure that out now
though :D

- Gunnar

On Sat, 8 Jan 2022 at 08:56, Nicholas Car <
[email protected]> wrote:

> Hi Robert,
>
> Marijane is correct! My favorite way of using variables in RDFlib SPARQL
> queries is to use string replace() so that I don't have to double-up {  and
> }, so I would do something like...
>
> ...I'll put a complete answer in if you re-post this to Stack Overflow
> please! That's where we like to keep all our RDFlib How-To questions! This
> list's just for RDFlib development.
>
> Cheers,
>
> Nick
>
>
>
> On Sat, Jan 8, 2022 at 5:35 AM Marijane White <[email protected]> wrote:
>
>> Hello Robert,
>>
>>
>>
>> You’ll need to use one of Python’s string formatting features, which
>> provide functionality for inserting variables into strings. The current
>> state of the art are f-strings, but note that you will have to double all
>> the curly braces in your query string if you use them because f-strings use
>> curly braces to denote variable names.
>>
>>
>>
>> Here’s a nice article from Real Python about all three ways to format
>> strings in Python, and their various pros and cons.
>>
>> https://realpython.com/python-f-strings/
>>
>>
>>
>>
>>
>> *Marijane White, M.S.L.I.S.*
>>
>> Data and Research Engagement Librarian, Assistant Professor
>>
>> Oregon Health & Science University Library
>>
>>
>>
>> *Email:* [email protected]
>>
>> *ORCiD:* https://orcid.org/0000-0001-5059-4132
>>
>>
>>
>>
>>
>> *From: *<[email protected]> on behalf of Robert Alexander <
>> [email protected]>
>> *Reply-To: *"[email protected]" <[email protected]>
>> *Date: *Friday, January 7, 2022 at 11:17 AM
>> *To: *rdflib-dev <[email protected]>
>> *Subject: *[EXTERNAL] [rdflib-dev] SPARQLWrapper: how to interpolate
>> python variables in query
>>
>>
>>
>> Dear friends,
>>
>> I am a beginner so please bear with me.
>>
>> I find the """ """ style of writing the queries in my code very readable
>> but I am not able to introduce a variable in it.
>>
>> For example:
>>
>>
>>
>> sparql.setQuery("""
>>
>>
>>
>> select distinct ?id ?eurovoc
>>
>>
>>
>> where {
>>
>>
>>
>> ?atto a ocd:aic .
>>
>>
>>
>> ?atto dc:identifier ?id .
>>
>>
>>
>> ?atto ocd:startDate ?datapres .
>>
>>
>>
>> ?atto <http://purl.org/dc/terms/subject>
>> <https://urldefense.com/v3/__http:/purl.org/dc/terms/subject*3E__;JQ!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8S-ReoEb8$>
>> ?eurovoc .
>>
>>
>>
>> FILTER(REGEX(?datapres,'2021\\\\d{4}'))
>>
>>
>>
>> }
>>
>>
>>
>> """)
>>
>>
>>
>>
>>
>>
>>
>> in the above query I would like 2021 to be instantiated by a "year"
>> variable I previously set in the code.
>>
>>
>>
>>
>>
>>
>>
>> Any suggestions? Thank you very much and happy 2022
>>
>>
>>
>> Robert
>>
>>
>>
>> --
>> http://github.com/RDFLib
>> <https://urldefense.com/v3/__http:/github.com/RDFLib__;!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8ScFdqFBQ$>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "rdflib-dev" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/rdflib-dev/e6752549-3d70-413d-9844-338a76c2cfd2n%40googlegroups.com
>> <https://urldefense.com/v3/__https:/groups.google.com/d/msgid/rdflib-dev/e6752549-3d70-413d-9844-338a76c2cfd2n*40googlegroups.com?utm_medium=email&utm_source=footer__;JQ!!Mi0JBg!bxw1FRIizJnQ0o2yyvMLGOJADoq06M086Tbb4tbYH8JWoNveq21AnC8SIxGTAwM$>
>> .
>>
>> --
>> http://github.com/RDFLib
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "rdflib-dev" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/rdflib-dev/FB564366-1FE4-472F-84DE-75B21EA21873%40ohsu.edu
>> <https://groups.google.com/d/msgid/rdflib-dev/FB564366-1FE4-472F-84DE-75B21EA21873%40ohsu.edu?utm_medium=email&utm_source=footer>
>> .
>>
> --
> http://github.com/RDFLib
> ---
> You received this message because you are subscribed to the Google Groups
> "rdflib-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rdflib-dev/CAP7nqh18O_V5xibag0TJiDsoNVTOJHrSYQmyOZnt%3DHJBUF6-TA%40mail.gmail.com
> <https://groups.google.com/d/msgid/rdflib-dev/CAP7nqh18O_V5xibag0TJiDsoNVTOJHrSYQmyOZnt%3DHJBUF6-TA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
http://gromgull.net

-- 
http://github.com/RDFLib
--- 
You received this message because you are subscribed to the Google Groups 
"rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rdflib-dev/CAGm1ODnOe54_3YKro_vGfs%3D%2BRRq_RqychG8jWFr5x5uVkx9BWQ%40mail.gmail.com.

Reply via email to