RDFLib / sparqlwrapper

A wrapper for a remote SPARQL endpoint
https://sparqlwrapper.readthedocs.io/
Other
519 stars 123 forks source link

Issue with comma in SPARQL Prefixed Name in Virtuoso #94

Open RawStewage opened 6 years ago

RawStewage commented 6 years ago

Hi so I've been trying to run the following query

    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    sparql.setQuery("""
    SELECT ?article, ?title WHERE {
    ?article ?relation dbc:Victoria\\,_British_Columbia.
    ?article foaf:isPrimaryTopicOf ?title
    }
    """)
    sparql.setReturnFormat(JSON)
    results = sparql.query()

Which results in the following error

b"Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\\' (0x5c) in SPARQL expression at '\\'\n\nSPARQL query:\n\n#output-format:application/sparql-results+json\n\n    SELECT ?article, ?title WHERE {\n    ?article ?relation dbc:Victoria\\,_British_Columbia.\n    ?article foaf:isPrimaryTopicOf ?title\n    }\n    "

I'm not sure how to proced as just leaving the coma unquoted also results in an error of.

b"Virtuoso 37000 Error SP030: SPARQL compiler, line 5: syntax error at '_British_Columbia' before '.'\n\nSPARQL query:\n\n#output-format:application/sparql-results+json\n\n    SELECT ?article, ?title WHERE {\n    ?article ?relation dbc:Victoria,_British_Columbia.\n    ?article foaf:isPrimaryTopicOf ?title\n    }\n    "

Any advice would be very much appreciated

dayures commented 6 years ago

Some details:

TallTed commented 5 years ago

Please retitle this issue, from "Issue with coma in relation" to "Issue with comma in SPARQL Prefixed Name in Virtuoso"

fetahokey commented 4 years ago

Parentheses, coma, or any non-alphanumeric characters aren't legal in prefixed names, but you can just use the full URI instead:

SELECT ?birthPlace
WHERE {
    <http://dbpedia.org/resource/Tom_Johnston_(musician)> dbpprop:birthPlace ?birthPlace   
}

It's also possible to escape them using \:

SPARQL local names also allow the non-alphanumeric characters allowed in IRIs via backslash character escapes (e.g. ns:id\=123).

SELECT ?birthPlace
WHERE {
    dbpedia:Tom_Johnston_\(musician\) dbpprop:birthPlace ?birthPlace   
}

source: stackoverflow