-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexec_sql.py
More file actions
32 lines (22 loc) · 871 Bytes
/
exec_sql.py
File metadata and controls
32 lines (22 loc) · 871 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
from __future__ import annotations
from sqlite3 import connect
from typing import Optional
from arclet.alconna import Alconna, Args, Option
db = connect('example.db')
cursor = db.cursor()
select = Alconna(
"SELECT",
Args.columns(str, multiple=True),
Option("FROM", Args.table(str, "UNKNOWN")),
Option("WHERE", Args.conditions(str, multiple=True, kw_only=True)),
)
@select.bind()
def exec_sql(columns: tuple[str, ...], table: str, conditions: Optional[dict[str, str]] = None):
if table == "UNKNOWN":
print("Table name is required.")
return
if conditions is None:
conditions = {}
cursor.execute(f"SELECT {', '.join(columns)} FROM {table} WHERE {' AND '.join(f'{k}={v}' for k, v in conditions.items())}")
print(cursor.fetchall())
select.parse("SELECT health name FROM entertainment.pets WHERE USERID=MYID()")