VWED_server/docs/sc/数据库相关.md

132 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```markdown
## 系统配置
#### 获取 settings.py 的配置
* 方法说明
获取 settings.py 的配置。
```python
def get_settings_config_value(key: str) -> str
```
* 输入参数
keystr 类型,是 settings.py 中所要获取配置的字段名。
* 输出参数
JSON 字符串。
* 异常
## 数据库
#### 执行 SQL 语句
* 方法说明
通过 SQL 命令操作 RDS 系统数据库,可用于创建自定义数据表。
```python
def jdbc_execute_sql(sql: str) -> bool
```
* 输入参数
sqlstr 类型,创建表的 SQL 语句。示例如下:
```sql
CREATE TABLE `test` (`id` bigint NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4
```
* 输出参数:
* True SQL 命令执行成功。
* False SQL 命令执行失败。
* 异常
本方法不抛出异常,方法内捕获到异常后,按执行失败的信息返回处理。
#### 使用 SQL 查询数据
* 方法说明
本方法可以使用自定义的 SQL 查询数据,并返回指定字段。
```python
def jdbc_query(sql: str) -> str
```
* 输入参数
sqlstr 类型SQL 语句。
* 输出参数是 JSON 格式的字符串。
假设查询 sql 参数为sql="select id, name, age from person_table"
则返回的结果,经 JSON 反序列化后,数据如下:
```json
[
{
"id": "1",
"name": "lilei",
"age": 14
}
]
```
* 异常
本方法不抛出异常。
#### 更新数据
* 方法说明
更新数据记录。更新内容和目标条件,都支持动态扩展,即可执行动态拼接的 SQL 语句。
```python
def jdbc_insert_or_update(sql: str, *sql_params: Any) -> int
```
* 输入参数
* sqlstr 类型SQL 语句字符串。
* sql_paramsAny 类型,可动态扩展的参数。
* 输出参数
int 类型0 表示更新失败,大于 0 的数字表示更新的记录数目。
* 使用示例:
```python
sql = "update employee set age = 25 where name = ? and id = ?"
jdbc_insert_or_update(sql, "bob", 123)
```
* 异常
本方法不抛出异常,异常捕获后只作日志记录。
#### 查询数据数量
* 方法说明
执行一条自定义 SQL 语句,以查询满足条件数据的数量。
```python
def jdbc_query_count(sql: str) -> Optional[int]
```
* 输入参数
* sqlstr 类型SQL 语句字符串。**注意,只能使用 select count 语句。**
```sql
select count(1) from person where age = 20
```
* 输出参数
Optional[int] 类型None 表示查询失败,其他数字表示查询到的记录数目。
* 异常
本方法不抛出异常,异常捕获后只作日志记录。
```