Programmatically Retrieve SQL results
This tutorial focuses on programmatically retrieving SQL query results to extract metadata using the Coactive API and Coactive SQL Engine functionality.
Step 1: Setting Up Authorization
Retrieve and setup API Credentials
- Refer to our API Authenication page
Step 2: Submitting a Query
To automate queries, use the Coactive API’s asynchronous query submission functionality.
Example Query Submission
Use the following curl command to submit a SQL query:
API Response
The API will respond with details including a query_id:
Step 3: Checking Query Status
To check the status of your query, use the queryId returned in the previous response:
Example Status Response
Step 4: Paginating Through Large Result Sets
Queries return a maximum of 10,000 results. To retrieve more, use SQL’s LIMIT and OFFSET clauses across multiple queries.
Using LIMIT and OFFSET
For example, if your query returns 50,000 matching rows, you would run 5 separate queries:
Increment OFFSET by 10,000 with each query. When resultCount in the status response is less than 10,000, you’ve reached the final page.
Step 5: Downloading Results as CSV
Alternatively, you can download the results as a CSV file:
Example Response
The response provides a temporary link to the CSV file:
Notes
- The download link expires 15 minutes after it is generated.
Example: Full Query Execution Workflow
-
Submit a query via POST.
-
Use the queryId to check the status of the query.
-
Once the status is “Completed,” download the results as a CSV file.
