Retrieve SQL Runner query results using just API

Is there a way to do this just using the APIs? (aka without using the UI?)

Solved Solved
0 2 1,336
1 ACCEPTED SOLUTION

Hey @italo_damato 

You could do something like this. I used the Python SDK for the example below. We can use create_sql_query to directly write the sql, then we can grab the slug from that response and pass it into run_sql_query. Would this work for your use-case?

 

import looker_sdk



####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
from looker_sdk import methods40, models40
sdk = looker_sdk.init40() # or init40() for v4.0 API

slug = sdk.create_sql_query(body=models40.SqlQueryCreate( model_name="MODEL",sql="SELECT users.age FROM demo_db.users AS users LIMIT 20")).slug
result = sdk.run_sql_query(slug=slug, result_format="csv")
print(result)

Please let us know if you have any questions. 

Thanks,

Eric

View solution in original post

2 REPLIES 2

Hey @italo_damato 

You could do something like this. I used the Python SDK for the example below. We can use create_sql_query to directly write the sql, then we can grab the slug from that response and pass it into run_sql_query. Would this work for your use-case?

 

import looker_sdk



####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
from looker_sdk import methods40, models40
sdk = looker_sdk.init40() # or init40() for v4.0 API

slug = sdk.create_sql_query(body=models40.SqlQueryCreate( model_name="MODEL",sql="SELECT users.age FROM demo_db.users AS users LIMIT 20")).slug
result = sdk.run_sql_query(slug=slug, result_format="csv")
print(result)

Please let us know if you have any questions. 

Thanks,

Eric

Hi @Eric_Lyons , I try to run above code but its giving the below error.

  File "<stdin>", line 1, in <module>

  File "/.local/lib/python3.6/site-packages/looker_sdk/sdk/api40/methods.py", line 9094, in create_sql_query

    transport_options=transport_options

  File ".local/lib/python3.6/site-packages/looker_sdk/rtl/api_methods.py", line 180, in post

    return self._return(response, structure)

  File ".local/lib/python3.6/site-packages/looker_sdk/rtl/api_methods.py", line 93, in _return