Hello-
I'm trying to follow the newbie examples on readthedocs and need to be able to
query with with a variable, but can't seem to figure out how. The examples
given in 4.2.2 do not work.
Here I provide a simple failure case:
```
Hopefully I'm really misunderstanding the examples here...
Here is a sample test case:
from rdflib import Graph, Literal, BNode, Namespace, RDF, URIRef
from rdflib.namespace import DC, FOAF
from rdflib.plugins.sparql import prepareQuery
g = Graph()
# Create an identifier to use as the subject for Donna.
donna = BNode()
# Add triples using store's add method.
g.add( (donna, RDF.type, FOAF.Person) )
g.add( (donna, FOAF.nick, Literal("donna", lang="foo")) )
g.add( (donna, FOAF.name, Literal("Donna Fales")) )
g.add( (donna, FOAF.mbox, URIRef("mailto:[email protected]")) )
print(g.serialize(format='nt'))
q = prepareQuery('SELECT ?hash WHERE { ?hash <http://xmlns.com/foaf/0.1/name>
"Donna Fales" .}')
person = "Donna Fales"
q2 = prepareQuery('SELECT ?hash WHERE { ?hash <http://xmlns.com/foaf/0.1/name>
?person .}')
#
# Example query works
#
qres = g.query(q)
if qres:
print("Found ?hash equal to:")
print(qres.result)
#
# Example initBindings does not work
#
qres2 = g.query(q2, initBindings={'person': person})
if qres2:
print("Found ?hash equal to:")
print(qres2.result)
else:
print("{} not found in rdf - initBindings failed :(".format(person))
```
and the results:
```
_:N87622270e0574740bdea75e1bc609595 <http://xmlns.com/foaf/0.1/name> "Donna
Fales" .
_:N87622270e0574740bdea75e1bc609595 <http://xmlns.com/foaf/0.1/mbox>
<mailto:[email protected]> .
_:N87622270e0574740bdea75e1bc609595 <http://xmlns.com/foaf/0.1/nick>
"donna"@foo .
_:N87622270e0574740bdea75e1bc609595
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://xmlns.com/foaf/0.1/Person> .
Found ?hash equal to:
[(rdflib.term.BNode('N87622270e0574740bdea75e1bc609595'),)]
Donna Fales not found in rdf - initBindings failed :(
[Finished in 0.805s]
```
While the above code demonstrates searching with explicit terms 2,3, in a
triple (1, 2, 3) returns the expected corresponding triple, declaring x=3 and
searching 2,x fails to return anything. This seems like a trivial example -
what am I doing wrong?
The corresponding example in the documentation code
(http://rdflib.readthedocs.io/en/stable/intro_to_sparql.html ) also fails, and
I dont know where else to turn :(
Help!
--
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rdflib-dev/966d1997-8ebb-4740-826d-fbc2f84ddfec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.