-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunSQL.py
More file actions
38 lines (26 loc) · 857 Bytes
/
runSQL.py
File metadata and controls
38 lines (26 loc) · 857 Bytes
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
import sys
from tabulate import tabulate
import traceback
import os
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'backend'))
from database import Database # type: ignore
if len(sys.argv) <= 1:
raise ValueError("Enter name of SQL file to run. Usage: python runSQL.py [file].sql")
if __name__ == "__main__":
FILE = sys.argv[1]
with open(FILE) as f:
query = f.read()
db = Database(show_logs=False)
try:
db.run(query)
except Exception as e:
print(f"Error when running {FILE}\n")
traceback.print_exc()
exit(1)
db.commit()
try:
rows=db.fetch_all()
column_names = [desc[0] for desc in db.cursor.description]
print(tabulate(rows, headers=column_names, tablefmt="psql"))
except Exception:
exit(0)