This repository was archived by the owner on Mar 7, 2025. It is now read-only.
forked from gptscript-ai/structured-data-querier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
50 lines (43 loc) · 1.37 KB
/
query.py
File metadata and controls
50 lines (43 loc) · 1.37 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
import os
import tempfile
import time
import duckdb
dbFile = os.getenv('DBFILE', None)
if dbFile is None:
print("Error, Please provide a database file.")
exit(0)
if not os.path.isabs(dbFile):
dbFile = os.environ['GPTSCRIPT_WORKSPACE_DIR'] + '/' + dbFile
if not os.path.exists(dbFile):
print("Error, Database does not exist.")
exit(0)
query = os.getenv('QUERY', None)
readonly = os.getenv('READONLY', 'false').lower() == 'true'
max_retries = 10
attempts = 0
success = False
while not success and attempts < max_retries:
try:
cursor = duckdb.connect(database=dbFile, read_only=readonly, config={'temp_directory': tempfile.gettempdir()})
success = True
except:
time.sleep(1)
attempts += 1
if success:
if query is not None:
try:
result = cursor.sql(query)
except Exception as e:
print(f"Error: {e}, please reformulate your query and try again")
exit(0)
if result is not None:
result.show(max_rows=10000000, max_width=10000000)
else:
print("Query returned no results, please try another query.")
exit(0)
else:
print("Error: No query specified, please provide a query.")
exit(0)
else:
print("Error: Failed to connect to database. Did you supply the correct database file?")
exit(0)