Skip to content

Commit

Permalink
📝 update example
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 21, 2023
1 parent 4411863 commit 63c8107
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
15 changes: 8 additions & 7 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,14 @@ $ python fuzzy.py /test_fuzzy foo bar

## Examples

| Name | File |
|---------------|------------------------------------------|
| Calculate | [calculate.py](./example/calculate.py) |
| Execute | [exec_code.py](./example/exec_code.py) |
| Request Route | [endpoint.py](./example/endpoint.py) |
| Image Search | [img_search.py](./example/img_search.py) |
| PIP | [pip.py](./example/pip.py) |
| Name | File |
|----------------|------------------------------------------|
| Calculate | [calculate.py](./example/calculate.py) |
| Execute | [exec_code.py](./example/exec_code.py) |
| Request Route | [endpoint.py](./example/endpoint.py) |
| Image Search | [img_search.py](./example/img_search.py) |
| PIP | [pip.py](./example/pip.py) |
| Database Query | [exec_sql.py](./example/exec_sql.py) |

## License

Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ $ python fuzzy.py /test_fuzzy foo bar

## 示例

| 名称 | 文件 |
|------|------------------------------------------|
| 计算 | [calculate.py](./example/calculate.py) |
| 执行代码 | [exec_code.py](./example/exec_code.py) |
| 请求路由 | [endpoint.py](./example/endpoint.py) |
| 图片搜索 | [img_search.py](./example/img_search.py) |
| PIP | [pip.py](./example/pip.py) |
| 名称 | 文件 |
|-------|------------------------------------------|
| 计算 | [calculate.py](./example/calculate.py) |
| 执行代码 | [exec_code.py](./example/exec_code.py) |
| 请求路由 | [endpoint.py](./example/endpoint.py) |
| 图片搜索 | [img_search.py](./example/img_search.py) |
| PIP | [pip.py](./example/pip.py) |
| 数据库查询 | [exec_sql.py](./example/exec_sql.py) |


## 许可
Expand Down
29 changes: 29 additions & 0 deletions example/exec_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from sqlite3 import connect
from typing import Optional
from arclet.alconna import Alconna, Arg, Option, KeyWordVar, MultiVar

db = connect('example.db')

cursor = db.cursor()


select = Alconna(
"SELECT",
Arg("columns", MultiVar(str)),
Option("FROM", Arg("table", str, field="UNKNOWN")),
Option("WHERE", Arg("conditions", MultiVar(KeyWordVar(str)))),
)


@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()")

0 comments on commit 63c8107

Please sign in to comment.