Database 操作
更新时间:2026-09-01
创建数据库
功能介绍
新建一个库,用于进一步创建各类数据表。调用方法为 MochowClient.create_database(database_name, config=None)。
请求示例
Python
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户 API 密钥"
7endpoint = "您的实例访问端点" # 例如:http://127.0.0.1:5287
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15try:
16 database = client.create_database("db_test")
17 print(database.database_name)
18finally:
19 client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | String | 是 | 指定库的名称。库名称支持大小写字母、数字和下划线(_),必须以字母开头,长度限制为 1~255。 |
| config | Configuration 或 dict | 否 | 本次调用使用的配置;不传时使用客户端初始化时的配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| database | Database | 创建成功后的库对象,可通过 database.database_name 获取数据库名称。 |
删除数据库
功能介绍
删除指定的目标数据库。仅支持删除空库,不支持递归删除仍包含数据表的库;删除前需要先删除该数据库中的所有表,否则服务端返回错误。
调用方法为 MochowClient.drop_database(database_name, config=None)。SDK 会先查询数据库列表并获取目标数据库对象,再发起删除操作;目标库不存在时抛出 ClientError。
请求示例
Python
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户 API 密钥"
7endpoint = "您的实例访问端点" # 例如:http://127.0.0.1:5287
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15try:
16 client.drop_database("db_test")
17finally:
18 client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | String | 是 | 指定库的名称。 |
| config | Configuration 或 dict | 否 | 本次调用使用的配置;不传时使用客户端初始化时的配置。 |
返回参数
drop_database() 无返回值。删除失败时 SDK 抛出异常。
查询数据库列表
功能介绍
查询当前实例中的数据库列表。调用方法为 MochowClient.list_databases(config=None)。
请求示例
Python
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户 API 密钥"
7endpoint = "您的实例访问端点" # 例如:http://127.0.0.1:5287
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15try:
16 databases = client.list_databases()
17 for database in databases:
18 print(database.database_name)
19finally:
20 client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| config | Configuration 或 dict | 否 | 本次调用使用的配置;不传时使用客户端初始化时的配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| databases | List<Database> | 库对象列表。 |
Database
| 字段 | 字段类型 | 字段含义 |
|---|---|---|
| database_name | String | 数据库名称,可通过 database.database_name 获取。 |
获取数据库对象
功能介绍
调用 MochowClient.database(database_name, config=None) 获取指定名称的 Database 对象,以便继续执行表、索引、别名和数据操作。该方法会查询数据库列表并匹配库名;目标库不存在时抛出 pymochow.exception.ClientError。
请求示例
Python
1import pymochow
2from pymochow.auth.bce_credentials import BceCredentials
3from pymochow.configuration import Configuration
4
5account = "root"
6api_key = "您的账户 API 密钥"
7endpoint = "您的实例访问端点" # 例如:http://127.0.0.1:5287
8
9config = Configuration(
10 credentials=BceCredentials(account, api_key),
11 endpoint=endpoint,
12)
13client = pymochow.MochowClient(config)
14
15try:
16 database = client.database("db_test")
17 print(database.database_name)
18finally:
19 client.close()
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| database_name | String | 是 | 需要获取的数据库名称。 |
| config | Configuration 或 dict | 否 | 本次调用使用的配置;不传时使用客户端初始化时的配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| database | Database | 匹配到的库对象。 |
评价此篇文章
