# Query (select and paths) named graphs via pystardog

**URL:** <https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748>\
**Category:** Support\
**Created:** [December 6, 2023, 4:29pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748 "2023-12-06T16:29:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.stardog.com/u/Thomas)\
**Post date:** [December 6, 2023, 4:29pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/1 "2023-12-06T16:29:52Z")

</div>

Hi everbody,  
I try to send **select** and **paths** -queries via pystardog.  
Example python-code:  
\*with stardog.Connection(self.stardog\_database, \*_self.conn\_details) as conn:_

- 

```
       sparql_res_dict = conn.select(sparql_query,*

```

- 

```
                                    content_type=stardog.content_types.CSV,*

```

- 

```
                                    default_graph_uri=["urn:named_graph/me/tco/plant_0780_year_2023"])*

```

The triples are stored in the named graph "urn:named\_graph/me/tco/plant\_0780\_year\_2023".  
To clarify the data structure in the db see the result from stardog studio:  
query:  
SELECT \* WHERE  
{  
GRAPH ?g {  
{?s ?p ?o}  
}  
}  
Result:

| urn:named\_graph/me/tco/plant\_0780\_year\_2023 | [http://www.semantic.bosch.com/ae-testing/2022/8/product\_costing\_01](http://www.semantic.bosch.com/ae-testing/2022/8/product_costing_01) | rdf:type | owl:Ontology |
| --- | --- | --- | --- |
| urn:named\_graph/me/tco/plant\_0780\_year\_2023 | pco:Costcenter\_1234567 | rdf:type | pco:CostCenter |
| ... | | | |
| The above python code returns no results. | | | |
| If I add | | | |
| FROM urn:named\_graph/me/tco/plant\_0780\_year\_2023 | | | |
| to the query in the python code, it does return results. | | | |
| To my understanding the parameter | | | |
| default\_graph\_uri=["urn:named\_graph/me/tco/plant\_0780\_year\_2023"] | | | |
| should work like the FROM ... in the query - but it does not. | | | |

The above is also true for a paths-query issued by stardog (see above) and stardog studio.

As a workaround I can insert the FROM statement into the select query.  
But for a path query this workaround is not possible - there i need the parameter  
default\_graph\_uri=["urn:named\_graph/me/tco/plant\_0780\_year\_2023"]  
Where is the error in using the default\_graph\_uri - parameter?  
Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![Lars\_Heling](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/lars_heling/32/1657_2.png) [@Lars\_Heling](https://community.stardog.com/u/Lars_Heling)\
**Post date:** [December 7, 2023, 2:21pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/2 "2023-12-07T14:21:01Z")

</div>

Hi Thomas,

I am not sure if I follow your code example since it is a bit difficult to read due to the formatting. You should be able to retrieve the from the named graph using the `default_graph_uri `. For example, if you insert data in the DB as follows

```auto
INSERT DATA
{
    GRAPH <urn:myNamedGraph> {
        :a :b :c .
    }
}

```

you should be able to retrieve the data from the named graph with

```auto
results = conn.select('select * { ?s ?p ?o }', default_graph_uri=["urn:myNamedGraph"])

```

I hope this clarifies your problem. If not, it would be helpful if you could share some details on the query you are trying to execute.

Best regards

Lars

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.stardog.com/u/Thomas)\
**Post date:** [December 7, 2023, 3:31pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/3 "2023-12-07T15:31:22Z")

</div>

Hi Lars,  
thanks for the quick reply.  
Actually, what you write is exactly what i do - with one difference: I do write the triples to the db using the following code:

```auto
def write_file_to_graph(self, graph_uri: str, file_name: str):
        with stardog.Connection(self.stardog_database, **self.conn_details) as conn:
            conn.begin()
            file_path = path.join(loadEnvVar('DAYDIR'), f'{file_name}_{date.today()}.ttl')
            conn.add(content=stardog.content.File(file_path), graph_uri=graph_uri)
            conn.commit()

```

where:  
graph\_uri = 'urn:named\_graph/me/tco/plant\_0780\_year\_2023'

I will try to insert test data using your insert-Data-query and see, if that makes a difference ...  
Regards,  
Thomas

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.stardog.com/u/Thomas)\
**Post date:** [December 8, 2023, 9:59am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/4 "2023-12-08T09:59:25Z")

</div>

Hi Lars,

i tried your solution but have to say, that it does not work.  
I added the data to a db where there are already some data in the default graph.  
So using stardog studio, i can differentiate between querying the default graph and the named graph  
"urn:myNamedGraph".  
Trying the same via the pystardog api always returns the data from the default graph. It seems that the parameter "default\_graph\_uri" is not effective.

Can you confirm that your solution does actually work (try-out done)?  
Or might there be an implementation error in the pystardog module?  
Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![Lars\_Heling](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/lars_heling/32/1657_2.png) [@Lars\_Heling](https://community.stardog.com/u/Lars_Heling)\
**Post date:** [December 8, 2023, 10:33am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/5 "2023-12-08T10:33:18Z")

</div>

Hi Thomas,

I can confirm that the `default_graph_uri` parameter works as expected for pystardog. For example, when I execute the following example script:

```python
import stardog
import stardog.content_types

conn_details = {
  'endpoint': 'http://localhost:5820',
  'username': 'user',
  'password': 'pw'
}

data_dg = """
[] rdfs:label "Default Graph" .
"""

data_ng = """
[] rdfs:label "Named Graph" .
"""

with stardog.Connection('testing', **conn_details) as conn:

  # Clear the data in the DB and add data to the default and named graph
  conn.begin()
  conn.clear()
  conn.add(content=stardog.content.Raw(data_dg, 'text/turtle', name='dg.ttl'))
  conn.add(content=stardog.content.Raw(data_ng, 'text/turtle', name='ng.ttl'), graph_uri="urn:ng")
  conn.commit()

  # Query the data
  results = conn.select('select ?o { ?a ?p ?o }', content_type=stardog.content_types.CSV)
  print(f"Default Graph: {results}")

  results = conn.select('select ?o { ?a ?p ?o }', default_graph_uri=["urn:ng"], content_type=stardog.content_types.CSV)
  print(f"Named Graph: {results}")

  results = conn.select('select ?o from <urn:ng> { ?a ?p ?o }', content_type=stardog.content_types.CSV)
  print(f"Named Graph (FROM): {results}")

  results = conn.select('select ?o from named <urn:ng> { graph ?g { ?a ?p ?o } }', content_type=stardog.content_types.CSV)
  print(f"Named Graph (FROM NAMED): {results}")

  results = conn.select('select ?o from stardog:context:local { ?a ?p ?o }', content_type=stardog.content_types.CSV)
  print(f"All local graphs: {results}")

```

I get the expected results:

```stdout
Default Graph: b'o\nDefault Graph\n'
Named Graph: b'o\nNamed Graph\n'
Named Graph (FROM): b'o\nNamed Graph\n'
Named Graph (FROM NAMED): b'o\nNamed Graph\n'
All local graphs: b'o\nDefault Graph\nNamed Graph\n'

```

You should be able to reproduce these results on a simple DB for testing.

Maybe one thing to check in your code is that the provided graph URIs are not encoded in `<...>`, that is in SPARQL you would use `FROM <urn:graph>` but in the `default_graph_uri ` parameter takes `urn:graph` instead.

Best regards  
Lars

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.stardog.com/u/Thomas)\
**Post date:** [December 8, 2023, 11:09am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/6 "2023-12-08T11:09:02Z")

</div>

Hello Lars,

again thanks a lot for this precise help.  
I run your script with one change - i used an already existing database.  
And my output corresponds to my former results (see line 2):

```auto
Default Graph: b'o\nDefault Graph\n'
Named Graph: b'o\nDefault Graph\n'
Named Graph (FROM): b'o\nNamed Graph\n'
Named Graph (FROM NAMED): b'o\nNamed Graph\n'
All local graphs: b'o\nDefault Graph\nNamed Graph\n'

```

So the problem seems not to be in the code but maybe in the database configuration?  
Regards,  
Thomas

---

<div class="post-metadata">

**Author:** ![Lars\_Heling](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/lars_heling/32/1657_2.png) [@Lars\_Heling](https://community.stardog.com/u/Lars_Heling)\
**Post date:** [December 8, 2023, 1:51pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/7 "2023-12-08T13:51:12Z")

</div>

Hi Thomas,

could you check which version of `pystardog` you are using? The feature was just recently added to version 0.17.0.

Best regards  
Lars

---

<div class="post-metadata">

**Author:** ![elegerpurecycle](https://avatars.discourse-cdn.com/v4/letter/e/7ea924/32.png) [@elegerpurecycle](https://community.stardog.com/u/elegerpurecycle)\
**Post date:** [December 8, 2023, 2:13pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/8 "2023-12-08T14:13:41Z")

</div>

This was not intended for me.

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.stardog.com/u/Thomas)\
**Post date:** [December 8, 2023, 2:22pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/9 "2023-12-08T14:22:37Z")

</div>

Hi Lars,

you saved my week 🙂  
I had the version 0.12.0. running.  
After upgrading to 0.17.0 I finally get results from my named graphs!  
Thanks a lot!  
Best regards,  
Thomas

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [January 29, 2024, 8:45pm UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/10 "2024-01-29T20:45:55Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [March 26, 2024, 10:19am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/11 "2024-03-26T10:19:01Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [June 14, 2024, 9:03am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/12 "2024-06-14T09:03:17Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [December 17, 2024, 6:52am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/13 "2024-12-17T06:52:35Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 19, 2025, 9:23am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/14 "2025-07-19T09:23:41Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [March 10, 2026, 11:58am UTC](https://community.stardog.com/t/query-select-and-paths-named-graphs-via-pystardog/4748/15 "2026-03-10T11:58:21Z")

</div>


