-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
93 lines (76 loc) · 2.81 KB
/
Copy pathexample.py
File metadata and controls
93 lines (76 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from typing import Optional
from og_query_runner import ReadQueryFromFile,RunAndSaveQuery,RunQuery,SaveQueryResults
def example_run_query():
instance_url = "https://your-instance-url.com"
query = "SELECT * FROM your_table"
api_key = "your_api_key"
try:
# Run the query
response = RunQuery(instance_url, query, api_key)
print("Query executed successfully.")
print(f"Response: {response}")
except ValueError as e:
print(f"ValueError: {e}")
except RuntimeError as e:
print(f"RuntimeError: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def example_save_query_results():
data = {
"query": "SELECT * FROM your_table",
"headers": ["column1", "column2"],
"result": [["x", "y"], ["a", "b"]]
}
file_path = "query_results"
try:
# Save the query results to a CSV file
SaveQueryResults(data, file_path)
print(f"Query results successfully saved to {file_path}.csv")
except ValueError as e:
print(f"ValueError: {e}")
except RuntimeError as e:
print(f"RuntimeError: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Example function to run a query, save the result, and handle errors.
def example_run_and_save_query():
instance_url = "https://your-instance-url.com"
query = "SELECT * FROM your_table"
api_key = "your_api_key"
file_path = "query_results"
try:
# Run the query and save the results to a CSV file
RunAndSaveQuery(instance_url, query, api_key, file_path)
print(f"Query results successfully saved to {file_path}.csv")
except ValueError as e:
print(f"ValueError: {e}")
except RuntimeError as e:
print(f"RuntimeError: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Example function to read and print query results from a file
def example_read_query_from_file(file_path: str):
try:
# Read query result from CSV file
result = ReadQueryFromFile(file_path)
print("Query Result:")
print(f"Headers: {result['headers']}")
print(f"Result: {result['result']}")
if 'query' in result:
print(f"Original Query: {result['query']}")
except ValueError as e:
print(f"ValueError: {e}")
except RuntimeError as e:
print(f"RuntimeError: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# main function to run the example functions
if __name__ == "__main__":
example_run_query()
example_save_query_results()
example_run_and_save_query()
example_read_query_from_file("query_results")
os.remove("query_results.csv")
os.remove("query_results.query")
print("Files removed successfully.")