Skip to content

快速开始

更新: 2025/9/6 字数: 0 字 时长: 0 分钟

加载配置文件

首先需要创建一个配置文件,一般存放在项目根目录下的config目录中,文件名为config.[ext],拓展名可以是yamljsontomlini。 创建完配置文件后,就可以通过下面的代码来加载配置文件。下面以json格式的配置文件为例。

python
from sch.config import Config

config = Config()
config.load_json()

此时配置文件就加载完成了,要获取全部配置可以调用config.config属性。

python
print(config.config)
输出结果
json
{
  "mysql": {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "password",
    "database": "test"
  }
}

如果要获取某个配置的值,可以调用config.get方法,键名可以是用.分隔的多级键名。例如下面获取mysql.host的值:

python
print(config.get('mysql.host'))
输出结果

localhost

连接数据库

可以直接通过刚才的config对象来连接数据库,示例如下:

python
from sch.mysql import MySQL

mysql = MySQL(config)