-
Notifications
You must be signed in to change notification settings - Fork 26
/
sql.py
55 lines (39 loc) · 1.24 KB
/
sql.py
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
from __future__ import annotations
import os
import sqlite3
import tabulate
from colorama import Fore
con = sqlite3.connect("cached.sqlite")
cur = con.cursor()
if os.name == "nt":
from colorama import just_fix_windows_console
just_fix_windows_console()
# shutup, i know this is bad, but it's just a quick script
def parse_input(query: str) -> sqlite3.Cursor | str:
try:
cursor = cur.execute(query)
except sqlite3.OperationalError as e:
return f"Invalid input {e}"
else:
return cursor
while True:
query = input(f"{Fore.CYAN}SQL> {Fore.RESET}")
if query.lower() in {"exit", "quit"}:
break
if query.lower() in {"cls", "clear"}:
if os.name == "nt":
os.system("cls")
elif os.name == "posix":
os.system("clear")
continue
result = parse_input(query)
if isinstance(result, str):
print(result)
continue
if r := result.fetchall():
headers = [description[0] for description in result.description]
table = tabulate.tabulate(r, headers=headers, tablefmt="psql")
print(table)
print(f"{Fore.WHITE}Query OK: {Fore.RED}{result.rowcount}{Fore.WHITE} rows affected{Fore.RESET}")
con.commit()
con.close()